Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the non Default constructor with assembly.CreateInstance

I need to call the Non default constructor when using assembly.CreateInstance. how?

like image 897
Lisa Avatar asked Jun 28 '10 00:06

Lisa


People also ask

Why use Activator CreateInstance?

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.

What is Activator CreateInstance?

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.

What is Activator CreateInstance in net core?

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.

What is the constructor to initialize a new instance of the Assembly class?

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.


1 Answers

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);
like image 98
Igor Zevaka Avatar answered Oct 13 '22 15:10

Igor Zevaka