Sub TestObjectCreation() ' Next statement assumes that the Person class is defined in ' an assembly named "ReflectionDemo". Dim ty As Type = Type.GetType("ReflectionDemo.Person") Dim o As Object = Activator.CreateInstance(ty) ' Prove that we created a Person. Console.WriteLine("A {0} object has been created", o.GetType.Name) End SubIf you want to call a constructor that takes one or more parameters, you must prepare an array of values:
Sub TestObjectCreation2() Dim ty As Type = Type.GetType("ReflectionDemo.Person") ' Use the constructor that takes two arguments. Dim params() As Object = {"Joe", "Doe"} ' Call the constructor that matches the parameter signature. Dim o As Object = System.Activator.CreateInstance(ty, params) End SubCreating an object through its constructor method is a bit more convoluted, but I'm reporting this technique here for the sake of completeness:
Sub TestObjectCreation3() Dim ty As Type = Type.GetType("ReflectionDemo.Person") ' Prepare the argument signature as an array of types (2 strings). Dim types() As Type = {GetType(System.String), GetType(System.String)} ' Get a reference to the correct constructor. Dim ci As ConstructorInfo = ty.GetConstructor(types) ' Prepare the parameters. Dim params() As Object = {"Joe", "Doe"} ' Invoke the constructor and assign the result to a variable. Dim o As Object = ci.Invoke(params) End Sub-----------------------------------------------------------------------