Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 3159 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > Reflection

Reflection

Article posted by Chikul on 12/18/2009 | Views: 2697 | Category: .NET Framework | Level: Beginner 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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:1 year(s)
Home page:http://knowledgeparlour.blogspot.com/
Member since:Friday, November 20, 2009
Level:Bronze
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

To see how these invocation works and infer when to use to what (i.e. Invoke and BeginInvoke)

In this article we will understand core reasons of why we should use LINQ. Three tier / N-Tier is now a standard in almost all projects. New architectures which are coming up like MVC, MVP have the fundamental base and thinking of 3-tier methodology. So we have termed the 3-tiers as SALT, in other words we can not stay with out it. LINQ (Pepper) on other hand is a new technology which helps us create execute queries against disparate data sources like ADO.NET, Custom objects, XML etc. So it’s not a needed thing (Pepper) but it does helps to remove lot of issues related with traditional 3-tier. So let’s make a nice Three-tier LINQ dish using SALT and PEPPER. So we will first understand Three-tier, issues with it and then see how LINQ helps us to improve the same.

Unit testing is validation and verification methodology where the developers test the individual units of source code. In this tutorial we will try to understand how we can do unit testing using NUNIT framework. This tutorial we will try to understand the 5 important steps to do unit testing using NUNIT.

This article delves into the feature (of SQL server) of returning the multiple resultsets through stored procedure and handling such resultsets in C# for LINQTOSQL.

In the previous article ,http://www.dotnetfunda.com/articles/article1727-sql-server-integration-with-clr-aka-sql-clr-part-i-introduction-and-back.aspx we discussed what is SQLCLR, why SQLCLR and the steps for employing SQL CLR into our work. This article discusses the steps for creating a stored procedure, deployment and testing the same. This is second write-up of multipart series on this subject.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:33:17 AM