Visual Basic: dynamically create objects using a string as the name

后端 未结 4 746
感动是毒
感动是毒 2020-12-06 19:25

Is there a way to dynamically create an object using a string as the class name?

I\'ve been off VB for several years now, but to solve a problem in another language,

4条回答
  •  伪装坚强ぢ
    2020-12-06 19:52

    Take a look at the Activator.CreateInstance(Type) method.

    If your input is the name of a class you should be able do this:

    Dim obj As Object = Activator.CreateInstance(GetType("Name_Of_Your_Class")) 
    

    You'll have to fiddle with the GetType call to make sure you give it enough information but for most cases just the name of the class should work.

提交回复
热议问题