【问题标题】:Which should I reference in NETPortable , can't find GetMethods我应该在 NETPortable 中引用哪个,找不到 GetMethods
【发布时间】:2016-03-06 11:02:05
【问题描述】:

我有一个便携式类库。

".NETPortable,Version=v4.5,Profile=Profile75"

代码

typeof(T).GetMethods()

有错误

cannot resolve symbol 'GetMethods'

我的项目.json

{
  "supports": {},
  "dependencies": {

  },
  "frameworks": {
    ".NETPortable,Version=v4.5,Profile=Profile75": { },
    "net541": { },
    "dotnet5.4": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "System.Reflection": "4.1.0-beta-23516",
        "System.Collections.Concurrent": "4.0.11-beta-23516"
      }
    }

  }
}

和csproj文件中的key属性

  <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>    
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <TargetFrameworkProfile>Profile75</TargetFrameworkProfile>
    <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>

【问题讨论】:

    标签: c# .net asp.net-core .net-core


    【解决方案1】:

    我不太了解该特定配置文件(并且使用该配置文件测试任何东西都会给我带来大量关于预定义类型不可用的错误),但我确实知道反射现在分为TypeTypeInfo

    你可能想要一个using 指令

    using System.Reflection;
    

    然后使用:

    typeof(T).GetTypeInfo().DeclaredMethods
    

    (使用DeclaredMethods property。)请注意,这只会返回给定类中声明的方法,而不是继承的方法,这可能不是您需要的。

    typeof(T).GetTypeInfo().GetMethods()
    

    后者声称受 PCL 支持,但可能不支持 - 根据我的经验,MSDN 中的版本信息变得越来越难以信任,DNX 变体增加了更多复杂性。

    【讨论】:

    • 谢谢。 typeof(T).GetTypeInfo().DeclaredMethods 有效
    • @chsword:太好了——当然,只要您只需要声明的方法!令人讨厌的是我自己无法重现它......我永远不清楚当无法找到预定义类型时问题出在哪里。
    • 例如? @乔恩斯基特
    • @chsword:用你的 project.json 尝试一个几乎空的小应用程序给了我类似“预定义类型'System.Object'未定义或导入”的错误,感觉非常致命对我来说。我已经验证 Profile75 存在于我的机器上,所以我不确定出了什么问题...
    • visual studio 2015 升级到更新 1?并确保 dnx 的版本。
    【解决方案2】:

    下面的例子对我有用:

    using System.Reflection;
    
    namespace mynamespace {
      class MyClass() {
         void test() {
           var tMethods = typeof(DSRObject).DeclaringType.GetRuntimeMethods();
         }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-07
      • 2011-02-22
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多