Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the methods in System.Reflection.RuntimeReflectionExtensions?

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.

like image 701
Jeppe Stig Nielsen Avatar asked Mar 19 '14 10:03

Jeppe Stig Nielsen


1 Answers

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.

like image 103
Michael Hoffmann Avatar answered Oct 24 '22 20:10

Michael Hoffmann