Create Class dynamically at runtime

后端 未结 4 1032
陌清茗
陌清茗 2020-12-30 15:51

I have a method like this: (this is a generic method, and DYNAMIC_CLASS_TYPE will be changed in situation to other situation)

Dim res = f.MyMeth         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-30 16:10

    A much simpler way is to use the System.Activator:

    Activator.CreateInstance(YourType, ParramArrayOfYourVariablesForNEW)
    

    So say I have a class (Test) that takes a single string in the initialization

    Public Class Test
        Public Sub New(byval MyString as string)
    ...
    

    You could create a new instance like this:

    Activator.CreateInstance(gettype(Test),"My String")
    

提交回复
热议问题