Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get private constructor in .NET Core?

In .NET Framework such method exists:

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

But in .NET Core (1.1) I found only extension, which doesn't give me private constructor:

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

So I'm wondering if I can somehow access and get private constructor in .NET Core.

like image 404
qwermike Avatar asked Oct 28 '25 05:10

qwermike


1 Answers

Use TypeInfo.DeclaredConstructors.

According to documentation it applies to .NET Core 1.1 (and else).

You are expected to use GetTypeInfo().DeclaredConstructors and LINQ filter to find the desired constructor.

like image 104
qwermike Avatar answered Oct 31 '25 04:10

qwermike