Instantiating a Constructor with Parameters in an Internal Class with Reflection

Instantiating a constructor with parameters in an internal class with reflection

The issue is that Activator.CreateInstance(Type, object[]) does not consider non-public constructors.

Exceptions

MissingMethodException: No matching
public constructor was found.

This is easily shown by changing the constructor to publicvisibility; the code then works correctly.

Here's one workaround (tested):

 BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
CultureInfo culture = null; // use InvariantCulture or other if you prefer
object instantiatedType =
Activator.CreateInstance(typeToInstantiate, flags, null, parameter, culture);

If you only require the parameterless constructor this will work as well:

//using the overload: public static object CreateInstance(Type type, bool nonPublic)
object instantiatedType = Activator.CreateInstance(typeToInstantiate, true)

Instantiate Type with internal Constructor with reflection

BindingFlags:

var ctor = typeof(MyType).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(c => !c.GetParameters().Any());

var instance = (MyType)ctor.Invoke(new object[0]);

The BindingFlags gets the non public constructors. The specific constructor is found via specified parameter types (or rather the lack of parameters). Invoke calls the constructor and returns the new instance.

How to create an object instance of class with internal constructor via reflection?

It is not that impossible. you've to tell it is not a public.

var myClass = Activator.CreateInstance(typeof(MyClass), true);//say nonpublic

Instancing a class with an internal constructor

A FormatterServices.GetUninitializedObject method exists (Namespace: System.Runtime.Serialization), it supposedly calls no constructors, if you really want to try out that approach.

Instantiating Internal class with private constructor

First of all, the very fact that it is internal means you're not supposed to be doing what you want to do.

You should seriously try to find an alternate route to what you want to accomplish.

Unfortunately, you don't tell us what you want to accomplish, only the next step you think you want to do, so that's what I can help you with.

This code will work, and accesses the public static method:

Type t = typeof(SomeOtherTypeInSameAssembly)
.Assembly.GetType("ClassLibrary1.ABC");
MethodInfo method = t.GetMethod("Create",
BindingFlags.Public | BindingFlags.Static, null,
new Type[] { typeof(String) },
new ParameterModifier[0]);
Object o = method.Invoke(null, new Object[] { "test" });

Note that this relies on having access to another type in the same assembly, that is public. If you don't have that, you need to get hold of the Assembly object that contains the type.

Create instance using Activator

Activator only works with public constructors, with the simple overload of CreateInstance that you are using. For using private/protected/internal constructors, you need to use another overload. See https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance.

Can't Find Constructor on Generic Class Activator

Activator.CreateInstance() only looks for public constructors by default. If you want to look for other scopes of constructor, you need to use the overload with BindingFlags.

The flags needed are:

const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;

You probably also want to use InvariantCulture, so the overall call would look something like:

return Activator.CreateInstance(type, flags, null, new object[] {myArgs}, CultureInfo.InvariantCulture;)


Related Topics



Leave a reply



Submit