[英]C# Reflection, using MakeGenericMethod with method that has the 'new()' type constraint
[英]Win8 (WINRT) C# not allowing Generic Reflection using MakeGenericMethod
对于Windows 8(WinRT / Metro)使用Windows 2012 C#,我遇到以下问题:
public class HelperClass
{
public static bool test()
{
return true;
}
public static bool test1<T>()
{
return true;
}
public void runTestMethod()
{
Type[] tt = new Type[]{};
object[] param1 = new object[] {};
MethodInfo method = typeof(HelperClass).GetRuntimeMethod("test", tt);
//This works just fine
object o = method.Invoke(null,param1)
}
public void runTestMethod1(PropertyInfo p)
{
Type[] tt = new Type[]{};
object[] param1 = new object[] {};
MethodInfo method = typeof(HelperClass).GetRuntimeMethod("test", tt);
MethodInfo generic = method.MakeGenericMethod(p.GetType());
//This breaks with the error below
object o = generic.Invoke(null,param1)
}
}
The API 'D20OGL.BusinessObjects.HelperClass.test[RuntimePropertyInfo]()' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.
我正在编写的代码在很大程度上依赖于反射和泛型。 任何人都知道解决此问题的方法吗???
谢谢!
你几乎可以肯定的意思
method.MakeGenericMethod(p.PropertyType);
或可能
method.MakeGenericMethod(p.DeclaringType);
并不是
method.MakeGenericMethod(p.GetType());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.