Reflection

Chikul
Posted by in .NET Framework category on for Beginner level | Views : 5065 red flag

Reflection is one of the main features of .Net framework .
In short 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.(Metadata - Data about data).
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.

Introduction

Reflection is one of the main features of .Net framework. In short 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.(Metadata - Data about data). 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.

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. 

ex : 
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.
Page copy protected against web site content infringement by Copyscape

About the Author

Chikul
Full Name: Prasanti Prusty
Member Level: Bronze
Member Status: Member
Member Since: 11/20/2009 10:34:27 PM
Country: India

http://knowledgeparlour.blogspot.com/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)