2017-07-21 51 views
1

我不断收到错误:“类型”不包含定义“GetMethod”

CS1061: 'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?).

我试图构建Windows应用商店的应用程序!

这里是我的代码:

MethodInfo theMethod = itween.GetType().GetMethod (animation.ToString(), new Type[] { 
     typeof(GameObject), 
     typeof(Hashtable) 
}); 
theMethod.Invoke(this, parameters); 

回答

2

要在Windows商店应用中使用反射,所属类别类是用来代替类型类,这是经典的.NET应用程序中使用。

但是,它仍然有一些限制:

In a Windows 8.x Store app, access to some .NET Framework types and members is restricted. For example, you cannot call .NET Framework methods that are not included in .NET for Windows 8.x Store apps, by using a MethodInfo object.

参考:Reflection in the .NET Framework for Windows Store Apps

相当于你的代码段是这样的:

using System.Reflection; //this is required for the code to compile 

var methods = itween.GetType().GetTypeInfo().DeclaredMethods; 
foreach (MethodInfo mi in methods) 
{ 
    if (mi.Name == animation.ToString()) 
    { 
     var parameterInfos = mi.GetParameters(); 
     if (parameterInfos.Length == 2) 
     { 
      if (parameterInfos[0].ParameterType == typeof(GameObject) && 
       parameterInfos[1].ParameterType == typeof(Hashtable)) 
      { 
       mi.Invoke(this, parameters) 
      } 
     } 
    } 
} 

注意GetTypeInfo被定义为扩展方法,因此编译器需要using System.Reflection;才能识别此方法。