I want to get a System.Type given only the type name in a string.
For instance, if I have an object:
MyClass abc = new MyClass();
I can then say:
System.Type type = abc.GetType();
But what if all I have is:
string className = "MyClass";
I want to get a System.Type given only the type name in a string.
For instance, if I have an object:
MyClass abc = new MyClass();
I can then say:
System.Type type = abc.GetType();
But what if all I have is:
string className = "MyClass";
Type type = Type.GetType("foo.bar.MyClass, foo.bar");
MSDN. Make sure the name is Assembly Qualified.
Type type = Type.GetType("MyClass");
Make sure to include the namespace. There are overloads of the method that control case-sensitivity and whether an exception is thrown if the type name isn't found.
To create an instance of your class after you get the type, and invoke a method -
Type type = Type.GetType("foo.bar.MyClass, foo.bar");
object instanceObject = System.Reflection.Activator.CreateInstance(type);
type.InvokeMember(method, BindingFlags.InvokeMethod, null, instanceObject, new object[0]);