Monday 2 May 2011

Reflection in .NET

Reflection - The process of getting the metadata from modules/assemblies. When .NET code is compiled, metadata about the types defined in the modules is produced. These modules are in turn packaged as assemblied. The process of accessing this metadata in called Reflection.
The namespace System.Reflection contains classes that can be used for interrogating the types for a module/assembly. We use reflection for examining data type sizes for marshalling across process & machine boundaries.

.NET Framework's Reflection API allows you to fetch type (assembly) information at runtime programmatically. We can also achieve late binding by using .NET Reflection. At runtime, the Reflection mechanism uses the PE file to read information about the assembly. Reflection enables you to use code that is not available at compile time. .NET Reflection allows an application to collect information about itself and also to manipulate on itself. It can be used effectively to find all types in an assembly and/or dynamically invoke methods in an assembly. This includes information about the type, properties, methods, and events of an object. With Reflection, we can 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. We can also access attribute information using Reflection.

Assembly contain the modules, modules contain the types and type contain members. You can use the reflection to create the instance of the types dynamically. You can invoke the type's method dynamically and can access the fields information.

List of classes which we using in reflection:
  1. Assemly
  2. Module
  3. ConstructorInfo
  4. MethodInfo
  5. FieldInfo
  6. EventInfo
  7. PropertyInfo
  8. ParameterInfo

No comments:

Post a Comment