【问题标题】:Using Value of String variable to call an instance in vb.net winform使用字符串变量的值在 vb.net winform 中调用实例
【发布时间】:2012-11-12 16:10:46
【问题描述】:

我的 UserControl 名为“aaa”

然后我有变量:

Dim a as String = "aaa"

现在,我宣布

Dim uc as UserControl = new aaa

我的问题是,我可以使用下面的变量 a 的值在上面写声明

Dim uc as UserControl = new a

【问题讨论】:

    标签: vb.net winforms user-controls instance


    【解决方案1】:

    您可以使用反射(在System.Reflection)命名空间来做到这一点。例如:

    Dim t As Type = Assembly.GetExecutingAssembly().GetType("namespace.aaa")
    Dim o As Object = Activator.CreateInstance(t)
    

    请注意,您将需要完整的类型名称,包括命名空间,因此您可能需要将其连接到您的字符串,例如:

    Dim namespace As String = "MyNamespace"
    Dim t As Type = Assembly.GetExecutingAssembly().GetType(namespace & "." & a)
    Dim o As Object = Activator.CreateInstance(t)
    

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2014-01-22
      • 2017-08-08
      • 1970-01-01
      • 2018-05-21
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多