dynamically declare object vb.net
Imports System.Reflection Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Dim fullyQualifiedClassName as String = "System.Windows.Forms.TextBox" Dim fullyQualifiedClassName As String = "System.Windows.Forms.Button" Dim o = fetchInstance(fullyQualifiedClassName) ' sometime later where you can narrow down the type or interface... Dim b = CType(o, Control) b.Text = "test" b.Top = 10 b.Left = 10 Controls.Add(b) End Sub Private Function fetchInstance(ByVal fullyQualifiedClassName As String) As Object Dim nspc As String = fullyQualifiedClassName.Substring(0, fullyQualifiedClassName.LastIndexOf("."c)) Dim o As Object = Nothing Try For Each ay In Assembly.GetExecutingAssembly().GetReferencedAssemblies() If (ay.Name = nspc) Then o = Assembly.Load(ay).CreateInstance(fullyQualifiedClassName) Exit For End If Next Catch End Try Return o End Function
Source: stackoverflow.com