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)?
The easiest option is the
Activator.CreateInstanceoverload that accepts aTypeand aparams object[]. For theType, you can sometimes useType.GetType(string), but this doesn’t check all assemblies (just the current assembly and some system assemblies). If the name is assembly-qualified you’ll probably be OK – but if it is just namespace-qualified (i.e. FullName) then you’ll probably want to useAssembly.GetType(string)– i.e.