What is .NET Reflection? why and when we use it ?

.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.
Assemblies contain modules, modules contain types, and types contain members.

At runtime, the Reflection mechanism uses the PE file to read information about the assembly.


The System.Reflection namespace contains the classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types; this process is known as Reflection in .NET framework. We will take a look at some of the commonly used classed here:
ClassDescription
AssemblyRepresents an assembly, which is a reusable, versionable, and self-describing building block of a Common Language Runtime application. This class contains a number of methods that allow you to load, investigate, and manipulate an assembly.
ModulePerforms Reflection on a module. This class allows you to access a given module within a multi-file assembly.
AssemblyNameThis class allows you to discover numerous details behind an assembly's identity. An assembly's identity consists of the following:
  • Simple name
  • Version number
  • Cryptographic key pair
  • Supported culture
EventInfoThis class holds information for a given event. Use the EventInfo class to inspect events and to bind to event handlers.
FieldInfoThis class holds information for a given field. Fields are variables defined in the class. FieldInfo provides access to the metadata for a field within a class, and provides dynamic set and get functionality for the field. The class is not loaded into memory until Invoke or get is called on the object.
MemberInfoThe MemberInfo class is the abstract base class for classes used to obtain information about all members of a class (constructors, events, fields, methods, and properties).
MethodInfoThis class contains information for a given method.
ParameterInfoThis class holds information for a given parameter.
PropertyInfoThis class holds information for a given property.
Before we start using Reflection, it is necessary to understand the System.Type class.

Comments