Namespace: System.Reflection
Creating Objects Dynamically:
String AssemblyPath =
@“C:\windwos\....\framework\v2……\mscorlib.dll”;
Assembly asmMSCORLIB =
Assembly.LoadFile(AssemblyPath);
Type hashTableType =
asmMSCORLIB.GetType(“System.Collections.HashTable”);
Type [] constructorArgs =
Type.EmptyTypes;
OR
Type [] constructorArgs = new Type[]
{ typeof(int) };
ConstructorInfo hashTableConstructor
= hashTableType.GetConstructor( constructorArgs);
Object hashTable =
hashTableConstructor.Invoke( new object[] {} );
Invoking Methods:
MethodInfo hashTableAddMethod =
hashTableType.GetMethod(“ADD”);
hashTableAddMethod.Invoke(hashTable,
new object[] {“ThisIsKey”, “ItsValue”});
PropertyInfo hashTableCountProperty
= hashTableType.GetProperty(“Count”);
Int TotalElements = (int)
prop.GetValue(newHash, null);
Invoking Static Methods:
Type ConsoleType = typeof(console);
MethodInfo ConsoleDotWriteLine =
ConsoleType.GetMethod(“WriteLine”,
new Type[]
{typeof(string)});
ConsoleDotWriteLine.Invoke(null, new
object[] { TotalElements.ToString() } );
For Fields use GetValue and SetValue
methods.
For EventInfo use AddEventHandler
and RemoveEventHandler.
Comments