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 »

One Response to “System.Activator”

  1. ActiveEngine Sensei Says:

    You might be interested in checking out some work done by TheCodeSlinger. Here are two links from a post on my blog:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: