Activator.CreateInstance creates specified type using that type's default constructor.
Imports System
Class DynamicInstanceList
Private Shared instanceSpec As String = "System.EventArgs;System.Random;System.Exception;"
Public Shared Sub Main()
Dim instances() As String = instanceSpec.Split(";")
Dim instlist As Array = Array.CreateInstance(GetType(Object), instances.Length)
Dim item As Object
For i As Integer = 0 To instances.Length -1
Console.WriteLine("Creating instance of: {0}", instances(i))
item = Activator.CreateInstance(Type.GetType(instances(i)))
instlist.SetValue(item, i)
Next i
For Each o As Object In instlist
Console.WriteLine("Type: {0}" + vbNewLine + "Value: {1}" + _
vbNewLine + "HashCode: {2}" + vbNewLine, _
o.GetType().FullName, o.ToString(), o.GetHashCode())
Next o
End Sub
End Class
Related examples in the same category