Reflection is one of the features of .Net framework and has greater importance during the development of large applications.
In brief it is a powerful way of collecting and manipulate information present in application’s assemblies and its metadata.
Metadata contain all the Type information used by the application. The ability to obtain information at runtime also makes it even more advantageous.
When reflection is used along with system.type, it allows the developer to get the valuable information about all the types and about the assemblies.
We can even create the instances and then invoke various types that are used across the application.
What is Reflection?
Reflection is the ability to find out information about objects, the application details (assemblies), its metadata at run-time.
This allows application to collect information about itself and also manipulate on itself.
It can be used effectively to find all the 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 and to invoke the methods of object Invoke method can be used too.
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.
If Attributes (C#) are used in application, then with help of reflection we can access these attributes.
It can be even used to emit Intermediate Language code dynamically so that the generated code can be executed directly.
How to use Reflection in our applications?
System.Reflection namespace contains all the Reflection related classes. These classes are used to get information from any of the class under .NET framework.
The Type class is the root of all reflection operations. Type is an abstract base class that acts as means to access metadata though the reflection classes.
Using Type object, any information related to methods, implementation details and manipulating information can be obtained.
The types include the constructors, methods, fields, properties, and events of a class, along with this the module and the assembly in which these information are present can be accessed and manipulated easily.
we can use reflection to dynamically create an instance of any type, bind the type to an existing object, or get the type from an existing object.
Once this is done appropriate method can be invoked, access the fields and properties.
This can be done by specifying the Type of object or by specifying both assembly and Type of the object that needs to be created.
By this the new object created acts like any other object and associated methods, fields and properties can be easily accessed.
With reflection we can also find out about various methods associated with newly created object and how to use these object.
To find out the attributes and methods associated with an object we can use the abstract class MemberInfo, this class is available under the namespace System.Reflection.