Create an instance of a class from a string

后端 未结 8 1516
别跟我提以往
别跟我提以往 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:31

    I know I'm late to the game... but the solution you're looking for might be the combination of the above, and using an interface to define your objects publicly accessible aspects.

    Then, if all of your classes that would be generated this way implement that interface, you can just cast as the interface type and work with the resulting object.

提交回复
热议问题