【问题标题】:What's the equivalent for getting the list of constructors in .NET Standard / Core?在 .NET Standard / Core 中获取构造函数列表的等价物是什么?
【发布时间】:2017-03-26 19:57:25
【问题描述】:

我正在尝试将我的 .NET 4 项目升级到 .NETStandard 和 Core,但找不到对应的:-

        var ctors = typeof(T).GetConstructors();

GetConstructors 是反射的一部分,因此似乎有意缺少或移动了支持...

谢谢。 西蒙。

【问题讨论】:

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


    【解决方案1】:

    在 .NET 标准 /Core 中,许多反射 api 被移动到特定的包 (system.reflection)。该包提供了Type类的扩展方法GetTypeInfo

    typeof(T).GetTypeInfo().DeclaredConstructors;
    

    【讨论】:

    • 是的,GetTypeInfo().DeclaredConstructors 就是答案。从属性返回 ConstructorInfo 枚举。
    【解决方案2】:

    很简单 - 只需添加 GetTypeInfo():

    var ctors = typeof(T).GetTypeInfo().DeclaredConstructors();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2016-12-31
      • 2020-10-05
      • 2011-12-25
      • 2010-10-02
      相关资源
      最近更新 更多