首页 文章

'Type'不包含'GetMethod'的定义

提问于
浏览
1

我一直在收到错误:

CS1061:'Type'不包含'GetMethod'的定义,并且没有扩展方法'GetMethod'接受类型'Type'的第一个参数(你是否缺少using指令或汇编引用?) .

我正在尝试为Windows应用商店应用构建!

这是我的代码:

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

1 回答

  • 2

    要在Windows应用商店应用中使用Reflection,请使用TypeInfo类而不是经典.NET应用程序中使用的Type类 .

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

    在Windows 8.x Store应用程序中,对某些.NET Framework类型和成员的访问受到限制 . 例如,您不能使用MethodInfo对象调用.NET 8.x Store应用程序中未包含的.NET Framework方法 .

    参考: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; 才能识别此方法 .

相关问题