1.

What is Reflection?

Answer»

Reflection is a process by which a computer program can monitor and modify its own structure and behavior. It is a way to explore the structure of assemblies at run time (CLASSES, resources, methods). Reflection is the capability to find out the information about objects, metadata, and application details (assemblies) at run-time. We need to INCLUDE System.Reflection namespace to perform reflections in C#.

Exmaple:

PUBLIC class MyClass {    public virtual int Add(int numb1, int numb2)    {                     return numb1 + numb2;    }    public virtual int Subtract(int numb1, int numb2)    {        return numb1 - numb2;    } } static void Main(string[] args) {    MyClass oMyClass = new MyClass();    //Type information.    Type oMyType = oMyClass.GetType();    //Method information.    MethodInfo oMyMethodInfo = oMyType.GetMethod("Subtract");    Console.WriteLine("\nType information:" + oMyType.FullName);    Console.WriteLine("\nMethod info:" + oMyMethodInfo.Name);                Console.Read(); }

Why we need Reflection

We need reflection for the following purposes:

  • To view attribute information at run time
  • To view the structure of assemblies at run time (classes, resources, methods)
  • It ALLOWS dynamic/late BINDING to methods and properties
  • In serialization, it is used to serialize and de-serialize objects
  • In web service, it is used to create and consume SOAP messages and also to generate     WSDL
  • Debugging tools can use reflection to examine the state of an object


Discussion

No Comment Found