Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating a .Net object by classname

Tags:

.net

I have an abstract class Step, and many descendant Step classes that I would like to instantiate based on an XML document.

As such I would like to create an instance of a particular step class based on the type in the XML document

Step type="GenerateReport" .... Step type="PrintReport" ....

How can I instantiate an object by specifying the classname (and ideally the parameters to be passed through to the constructor)?

like image 291
Alan Wolman Avatar asked Dec 22 '22 11:12

Alan Wolman


1 Answers

I think you simply want to use the Activator.CreateInstance method:

var object = Activator.CreateInstance(null, "Classname");
like image 173
Noldorin Avatar answered Jan 13 '23 14:01

Noldorin