system.type

Create an instance of a class from a string

时光毁灭记忆、已成空白 提交于 2019-11-25 22:41:47
问题 Is there a way to create an instance of a class based on the fact I know the name of the class at runtime. Basically I would have the name of the class in a string. 回答1: Take a look at the Activator.CreateInstance method. 回答2: Its pretty simple. Assume that your classname is Car and the namespace is Vehicles , then pass the parameter as Vehicles.Car which returns object of type Car . Like this you can create any instance of any class dynamically. public object GetInstance(string