Since .NET 4.5 (2012), some new extension methods show up, from System.Reflection.RuntimeReflectionExtensions
class. However, the new methods do not seem to give us anything new. An example:
static void Main()
{
var prop1 = typeof(string).GetProperty("Length");
var prop2 = typeof(string).GetRuntimeProperty("Length"); // extension, needs: using System.Reflection;
Console.WriteLine(prop1 == prop2);
Action a = Main;
var meth1 = a.Method;
var meth2 = a.GetMethodInfo(); // extension, needs: using System.Reflection;
Console.WriteLine(meth1 == meth2);
}
This writes True
twice.
(The ==
operator is overloaded here, but even checking for reference equality with (object)prop1 == (object)prop2
and (object)meth1 == (object)meth2
gives True
).
So what is the purpose of these new publicly visible methods? Clearly I must be overlooking or misunderstanding something.
The GetRuntime* methods are used for WinRT projects. Because the types used by WinRT may be different than the types used by .NET, but still function the same and have the same name, these reflection methods ensure that the correct MemberInfo is returned. You don't likely want a .NET MemberInfo at runtime if you're running WinRT.
See Hans Passant's comment on the original question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With