There are circumstances where we want to create an instance of an object using its name at runtime. In VB6, the CreateObject method would allow you to create an instance of an ActiveX object using its Prog ID.
In .Net, things are a little bit more complicated because objects have constructors. Before you can create an instance of an object, you have to choose which constructor to use.
Lets have two examples.
The first example will assume that the object in question implements a public constructor that has no parameters.
Public Shared Function CreateNew(ByVal ClassName As String) As Object
Dim objType As System.Type Dim objConstructor As Reflection.ConstructorInfo
' no classname provided
If ClassName Is Nothing Then Throw New ArgumentNullException("ClassName")
' find the type
objType = Type.GetType(ClassName)
If objType Is Nothing Then _Throw New Exception("Unknown Type " & ClassName)
' find the default constructor (the one that accepts no params)
Dim objParams() As System.Type = {}objConstructor = objType.GetConstructor(objParams)
If objConstructor Is Nothing Then _Throw New Exception("No default constructor for " & objType.FullName)
' invoke contsructor and return object
Return objConstructor.Invoke(objParams)
End Function
Public Shared Sub Test()
Dim objArray As ArrayList = CreateNew("System.Collections.ArrayList")
End Sub


