自.NET 4.5(2012)以来,一些新的扩展方法出现,从System.Reflection.RuntimeReflectionExtensions
class。但是,新方法似乎没有给我们任何新的东西。举例:System.Reflection.RuntimeReflectionExtensions中方法的用途是什么?
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);
}
这写两次True
。
(该==
运算符重载在这里,但即使(object)prop1 == (object)prop2
检查参考平等和(object)meth1 == (object)meth2
给True
)。
那么这些新公开可见方法的目的是什么?很显然,我必须忽略或误解某些东西。
我也不知道为什么这些方法是存在的,因为内部'GetRuntimeProperty'只是执行相同的'type.GetProperty(name)'方法 –
我看不到任何目的。它是完全多余的。如果你看看反编译的代码,你会看到他们是多么毫无意义。猜猜有人在进入代码库的那一天睡着了。 – leppie
这需要添加以支持WinRT。这是基于COM的,它不支持反射。 CLR内置的语言投影掩盖了许多怪癖,隐藏了基本类型系统。没有什么非常微妙的,在WinRT中,一个字符串是*非常*不同的类型。但它并不完美,不完全确定它们是如何效仿它的。 –