Monday, January 02, 2012

What is System.Reflection in .NET?

Posted by Rahul Kharde at 9:46 PM

“Reflection is a collection of classs”

system.refelection is namespace used to get name information about the assembly”

Reflection is the ability of the .NET framework to gather information (metadata) about assemblies, modules and types at runtime. It allows you also to dynamically create instances of types, invoke methods and access fields, properties and attributes.

Reflection

  • Ability to read metadata at runtime.
  • Using reflection, it is possible to uncover the methods, properties, and events of a type, and to invoke them dynamically.
  • Create new types at runtime.
  • Derived from namespace System.Reflection.


Example

Get the Version of the assembly

First add the reflection namespace

using System.Reflection;

Example 1

// get the version object for this assembly

Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

// write out the whole version number

Response.Write("Version = " + v.ToString());Output

Output

Version = 0.0.0.0


Example 2

Add assembly version attribute

using System.Reflection;

[assembly: AssemblyVersionAttribute("1.2.3.4")]

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

// get the version object for this assembly

Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

// write out the whole version number

Response.Write("Version = " + v.ToString());

}

}

Output

Version = 1.2.3.4



If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top