Instantiate a class from its textual name

后端 未结 2 850
生来不讨喜
生来不讨喜 2020-11-30 05:22

Don\'t ask me why but I need to do the following:

string ClassName = \"SomeClassName\";  
object o = MagicallyCreateInstance(\"SomeClassName\");
2条回答
  •  既然无缘
    2020-11-30 06:00

    Activator.CreateInstance(Type.GetType("SomeNamespace.SomeClassName"));
    

    or

    Activator.CreateInstance(null, "SomeNamespace.SomeClassName").Unwrap();
    

    There are also overloads where you can specify constructor arguments.

提交回复
热议问题