Wednesday, November 22, 2006

Creating an object just from its name in .NET

Recently one of my co-workers was looking for a method of creating an instance of an object just from its name. The Activator class has methods to do just that, but those methods need a full name. So to create an object just from its name (excluding version number, public key and assembly name), I came up with the following steps. Type type = Type.GetType("name of object"); //where name of object is System.String or namespace.object Object theObj = Activator.CreateInstance(type); Thats it!

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.