How can I get generic Type from a string representation?

前端 未结 5 746
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 00:42

I have MyClass.

And then I have this string s = \"MyClass\";. How can I get Type from the string s

5条回答
  •  难免孤独
    2020-11-30 01:28

    To just get the type object from the string, use:

    Type mytype = Type.GetType(typeName);
    

    You can then pass this to Activator.CreateInstance():

    Activator.CreateInstance(mytype);
    

提交回复
热议问题