2017-07-21 1 views
1

Je continue à obtenir l'erreur:« Type » ne contient pas de définition pour « 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?).

Je suis en train de construire pour les applications Windows Store!

Et voici mon code:

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

Répondre

2

Pour utiliser la réflexion dans l'application Windows Store, TypeInfo classe est utilisée à la place de la classe de type, qui est utilisé dans les applications .NET classiques.

Cependant, il est encore avec quelques restrictions:

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.

Référence: Reflection in the .NET Framework for Windows Store Apps

Un extrait de code correspondant à la vôtre est comme:

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) 
      } 
     } 
    } 
} 

Notez que GetTypeInfo est définie comme l'extension méthode, donc using System.Reflection; est nécessaire pour que le compilateur reconnaisse cette méthode.