Recently I used System.Activator class from the .Net framework class library. This class contains static methods to create types of objects locally or remotely, or obtain references to existing remote objects. The use of this class arises when you need to create an object of a type, and the type can only be determined at runtime. One of the most method in the class is CreateInstance. It creates an instance of the specified type using the constructor that best matches the specified parameters. If no valid constructor is found than a
MissingMethodException exception is thrown. This method has about 9 to 10 overloads.

The way I used this class is like this:

String typedefinition =  “Anubhav.PDF.ApplicationHelper,Anubhav.PDF” ; // this was read from the config file.

Type tPDFHelperType = Type.GetType(typedefintion);

object oPDFHelper = Activator.CreateInstance(tPDFHelperType);

if (oPDFHelper is IPDFHelper)

You can use Activator class to create objects for classes that are added after your code has been compiled and deployed, all you may want is a way to read a configuration/xml file which suggests what all objects need to be created.

Posted in .net. 1 Comment »