Getting a System.Type from type's partial name

前端 未结 7 865
梦谈多话
梦谈多话 2020-11-27 07:18

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 MyCl         


        
7条回答
  •  再見小時候
    2020-11-27 07:37

    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]);
    

提交回复
热议问题