How to get a Type from a string

0

For example, I want a string containing the "System.Windows.Forms.Button" information to return a New Button() button object, would that be possible?

  

Note: It has the C # tag as it is easy to translate from C # to Visual Basic

    
asked by anonymous 15.02.2016 / 23:10

1 answer

2

Use the CreateInstance function, informing you all the way to the class:

var obj = Activator.CreateInstance(Type.GetType("System.Windows.Forms.Button"));

You can also use a cast in the variable's creation for a class that you are sure is the parent of the one you are creating, for example:

var obj = (Control)Activator.CreateInstance(Type.GetType("System.Windows.Forms.Button"));

This way you can use the functions of class Control easily.

    
16.02.2016 / 00:04