C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Reflection in C#


Posted Date: 01 Jul 2008    Resource Type: Articles    Category: .NET Framework
Author: JeffMember Level: Gold    
Rating: Points: 10



This article is aimed to explain reflection in .NET Framework.



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.

As mentioned earlier, 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.


Example: The following statement fetches mscorlib assembly information



Assembly assemblyInformation = typeof(Object).Module.Assembly;



// Loads an assembly using its file name

Assembly assemblyInformation = Assembly.LoadFrom("MyApplicationToRu n.exe");



// Fetch the types available in the assembly

Type [] assemblyTypeInformation = assemblyInformation.GetTypes ();



foreach (Type TypeInformation in assemblyTypeInformation)

{

Console.WriteLine (TypeInformation.FullName);

}






Responses

Author: SSN    01 Jul 2008Member Level: Gold   Points : 0
nice article...
It got very helpful to us...


Author: SSN    01 Jul 2008Member Level: Gold   Points : 0
nice article...
It got very helpful to us...


Author: SSN    01 Jul 2008Member Level: Gold   Points : 2
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.

As mentioned earlier, 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.


Example: The following statement fetches mscorlib assembly information



Assembly assemblyInformation = typeof(Object).Module.Assembly;



// Loads an assembly using its file name

Assembly assemblyInformation = Assembly.LoadFrom("MyApplicationToRu n.exe");



// Fetch the types available in the assembly

Type [] assemblyTypeInformation = assemblyInformation.GetTypes ();



foreach (Type TypeInformation in assemblyTypeInformation)

{

Console.WriteLine (TypeInformation.FullName);

}


this one got very helpful to me.................



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Client server sample using WCF and net.tcp binding
Previous Resource: Key-Value Functionality in Combo Box
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use