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

Myk*_*kun 3 c# reflection .net-core

在.NET Framework中,这种方法存在:

public ConstructorInfo GetConstructor(
    BindingFlags bindingAttr,
    Binder binder,
    CallingConventions callConvention,
    Type[] types,
    ParameterModifier[] modifiers
)
Run Code Online (Sandbox Code Playgroud)

但是在.NET Core(1.1)中,我只找到了扩展名,这没有给我私有构造函数:

public static ConstructorInfo GetConstructor(this Type type, Type[] types)
Run Code Online (Sandbox Code Playgroud)

所以我想知道我是否可以通过某种方式访问​​并获得.NET Core中的私有构造函数。

Myk*_*kun 5

使用TypeInfo.DeclaredConstructors

根据文档,它适用于.NET Core 1.1(及其他)。

您应该使用GetTypeInfo().DeclaredConstructorsLINQ过滤器来查找所需的构造函数。