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

时间:2018-08-28 08:12:05

标签: c# reflection .net-core

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

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

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

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

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

1 个答案:

答案 0 :(得分:3)

使用TypeInfo.DeclaredConstructors

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

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

相关问题