Create an instance of a class from a string

后端 未结 8 1335
别跟我提以往
别跟我提以往 2020-11-22 02:53

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.

8条回答
  •  误落风尘
    2020-11-22 03:17

    To create an instance of a class from another project in the solution, you can get the assembly indicated by the name of any class (for example BaseEntity) and create a new instance:

      var newClass = System.Reflection.Assembly.GetAssembly(typeof(BaseEntity)).CreateInstance("MyProject.Entities.User");
    

提交回复
热议问题