I need to call the Non default constructor when using assembly.CreateInstance. how?
Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters. Creates an instance of the specified type using the constructor that best matches the specified parameters.
The Activator. CreateInstance method creates an instance of a type defined in an assembly by invoking the constructor that best matches the specified arguments. If no arguments are specified then the constructor that takes no parameters, that is, the default constructor, is invoked.
The Activator. CreateInstance method creates an instance of a specified type using the constructor that best matches the specified parameters. For example, let's say that you have the type name as a string, and you want to use the string to create an instance of that type.
A constructor with no parameters is called a default constructor. A default constructor has every instance of the class to be initialized to the same values. The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class.
Activator.CreateInstance
is a much friendlier API than Assembly.CreateInstance
to use for these kinds of things:
var type = Type.GetType("MyNamespace.MyClass, MyAssembly");
Activator.CreateInstance(type, constructorParam1, constructorParam2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With