簡體   English   中英

Win8(WINRT)C#不允許使用MakeGenericMethod進行通用反射

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM