Class VB 中的动态实例化 (System.Windows.Forms)

Dynamic Class Instantiation in VB (System.Windows.Forms)

我需要 VB 中的动态 class 实例化。我得到一个像 "System.Windows.Forms.TextBox" 这样的字符串,并根据该字符串动态地将我的对象类型设置为 TextBox。

我已经尝试了 "Activator.CreateInstance(...)" 和 "CallByName(...)",但我没能成功。

有人知道这是如何工作的吗?

谢谢

Public Function CreateClass(ByVal className As String) As Object

    Dim asms() As Assembly = AppDomain.CurrentDomain.GetAssemblies

    For Each Asm As Assembly In asms
        Dim types = Asm.GetTypes

        For Each T As Type In types
            If T.Name.Equals(className, StringComparison.OrdinalIgnoreCase) Then
                Return Activator.CreateInstance(T)
            End If
        Next

    Next

    Throw New Exception("Type not found")
End Function