Skip to main content

Posts

Showing posts with the label Reflection

Writing Dynamic Code using .NET Reflection

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...

Reflecting Types in .NET

Getting Types:                 Type Object can be obtained:                                 From Assembly class                                 From Module class                                 From instance of an object                                 Using typeof keyword in C#   ...