In .NET Framework
you can easily reflect methods. E.g.:
var methodInfo = typeof(object).GetMethod("MemberwiseClone", bindingFlags);
In a .NET Standard
project, however, the compiler complains:
error CS1061: 'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
Q: How do you perform equivalent reflection using .NET Standard
?
For almost all reflection in .NET Core 1.x, you need a TypeInfo
instead of Type
.
There's an extension method of GetTypeInfo
in the System.Reflection
namespace, so you want:
using System.Reflection; // For GetTypeInfo
...
var methodInfo = typeof(object).GetTypeInfo().GetMethod("MemberwiseClone", bindingFlags);
Note that TypeInfo.GetMethod()
doesn't exist .NET Standard earlier than 1.6, but TypeInfo.DeclaredMethods
has been in since 1.0.
.NET Standard 2.0 reintroduced members into System.Type
(as part of reinstating a large portion of the desktop framework into .NET Standard) so you don't need to go through this hoop when targeting 2.0+.
See more on this question at Stackoverflow