Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them.
// Using GetType to obtain type information: int i = 42; System.Type type = i.GetType(); System.Console.WriteLine(type);
(1):- Use Assembly to define and load assemblies, load modules that are listed in the assembly manifest, and locate a type from this assembly and create an instance of it.
(2): Use Module to discover information such as the assembly that contains the module and the classes in the module. You can also get all global methods or other specific, nonglobal methods defined on the module.
Dynamically invoking a method
namespace Programming_CSharp { using System; using System.Reflection; public class Tester { public static void Main( ) { Type theMathType = Type.GetType("System.Math"); Object theObj = Activator.CreateInstance(theMathType); // array with one member Type[] paramTypes = new Type[1]; paramTypes[0]= Type.GetType("System.Double"); // Get method info for Cos( ) MethodInfo CosineInfo = theMathType.GetMethod("Cos",paramTypes); // fill an array with the actual parameters Object[] parameters = new Object[1]; parameters[0] = 45; Object returnVal = CosineInfo.Invoke(theObj,parameters); Console.WriteLine( "The cosine of a 45 degree angle {0}", returnVal); } } }