Dynamic creation of an instance by name

I am trying to do some reflection which includes creating an object via reflection.  I know the name of the class, I just want to dynamically create the class via the name (and assembly if necessary.)  I don't want to switch on the text name of the class and hard code the creation of the class, I just want to get a reference to a new object of some type that I specify via a string.  Anyone done this   I know you can do this in Java simply enough, I would assume that there is a way to do this in C# but I can't find it in the documentation!

Your help would be appreciated!

Marty


Answer this question

Dynamic creation of an instance by name

  • Basel Alnachef

    Duh!  Thanks, I could not find this in the documentation for the life of me.  That answers the question.  A little tricky on the format of objects in different modules (i.e. the string parameter), but I got it figured out.  Thanks again!

    Marty

  • Nam Tran

    See http://forums.microsoft.com/msdn/ShowPost.aspx PostID=36311:

    Type t = Type.GetType(typeName);
    object o = Activator.CreateInstance(t);


  • Alex Chew

    Thanks.  It looks like I got the answer below.
  • Ken Davis

    post the java and maybe we can translate it

  • Dynamic creation of an instance by name