Dynamic Objects with Dynamic Code in C#

One of the capabilities that Reflection provides besides META data on the Assembly is the capability to create objects dynamically.

string path = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\" + "mscorlib.dll";
Assembly myAssembly = Assembly.LoadFile(path);
Type myHashType = myAssembly.GetType("System.Collections.Hashtable");
Type[] argumentTypes = Type.EmptyTypes;
ConstructorInfo ctor = myHashType.GetConstructor(argumentTypes);

NOTE: ConstructorInfo is a specialized MethodBase object that looks and acts like a typical method but always returns an instance of a specific type.

Type[] myArgumentTypes = new Type[] { typeof(int) };
ConstructorInfo ctor = hashType.GetConstructor(myArgumentTypes);
object myNewHash = ctor.Invoke(new object[] {});
MethodInfo meth = hashType.GetMethod("Add");
meth.Invoke(newHash, new object[] { "Hi", "Hello" });

If we try to evoke static member we don’t require having an instance of the object.