我将UserControl命名为'aaa'
然后我有变量:
Dim a as String =“aaa”
现在,我宣布
Dim uc as UserControl = new aaa
我的问题是,我可以使用下面的变量a的值来编写声明吗
Dim uc as UserControl = new a
答案 0 :(得分: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)