Create an instance of a class from a string

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

    Probably my question should have been more specific. I actually know a base class for the string so solved it by:

    ReportClass report = (ReportClass)Activator.CreateInstance(Type.GetType(reportClass));
    

    The Activator.CreateInstance class has various methods to achieve the same thing in different ways. I could have cast it to an object but the above is of the most use to my situation.

提交回复
热议问题