Building on what Itowlson wrote:
From MCTS: .net 2.0: Application Development Foundation
First get the assembly:
Dim path As String =
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\"
+ _ "mscorlib.dll"
Dim theAssembly As Assembly =
Assembly.LoadFile(path) Dim hashType
As Type =
theAssembly.GetType("System.Collections.Hashtable")
Once you have the type, you can ask it for a ConstructorInfo object to use to construct
your new type:
Dim argumentTypes() As Type =
Type.EmptyTypes ' Empty Constructor
Dim ctor As ConstructorInfo =
hashType.GetConstructor(argumentTypes)
The method represented in a ConstructorInfo object is a specialized MethodBase object
that looks and acts like a typical method but always returns an instance of a specific
type. In this example, you are asking the Type class to return an empty constructor.
(You are supplying an empty Array of Types to specify the empty constructor.) You
could also ask for a constructor with specific arguments by supplying an array of the
constructor argument types, like so:
Dim argumentTypes() As Type = _ New
Type() {GetType(System.Int32)} ' One
argument of type Int32 Dim ctor As
ConstructorInfo =
hashType.GetConstructor(argumentTypes)
Once you have the ConstructorInfo object, creating an object is as simple as invoking
the constructor. Here is how to invoke the empty constructor:
Dim newHash as Object =
ctor.Invoke(New Object() {})
Once you have an instance of an object, you simply use reflection to get the info class
you need to call, and then you invoke the info class to execute the code.
For example, call the Add method on your new Hashtable instance:
Dim meth As MethodInfo = hashType.GetMethod("Add")
meth.Invoke(newHash, New Object()
{"Hi", "Hello"})
You can now use the PropertyInfo class to get the count of the items in your
Hashtable to verify that the Add worked as you expected it to:
Dim prop As PropertyInfo =
hashType.GetProperty("Count")
Dim count As Integer =
CType(prop.GetValue(newHash,
Nothing),Integer)