代码之家  ›  专栏  ›  技术社区  ›  qwermike

我可以在.NET核心中获得私有构造函数吗?

  •  2
  • qwermike  · 技术社区  · 6 年前

    public ConstructorInfo GetConstructor(
        BindingFlags bindingAttr,
        Binder binder,
        CallingConventions callConvention,
        Type[] types,
        ParameterModifier[] modifiers
    )
    

    public static ConstructorInfo GetConstructor(this Type type, Type[] types)
    

    所以我想知道我是否能以某种方式访问并获取.NETCore中的私有构造函数。

    1 回复  |  直到 6 年前
        1
  •  7
  •   Patrick Hofman Wahid Bitar    6 年前

    使用 TypeInfo.DeclaredConstructors

    根据 documentation 它适用于.NETCore1.1(以及其他版本)。

    您需要使用 GetTypeInfo().DeclaredConstructors 和LINQ过滤器来找到所需的构造函数。