Reflection Explained
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   Reflextion
Current Lesson:
Reflection Explained
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Web.Config Template Reference [next Lesson]  XML In ASP.NET - The XmlTextReader
Reflection Explained
  by: Salman Ahmed

So reflection enables you to find out information about types in your assemblies during runtime. Using reflection, you can find out the details of an object's methods in terms of its access modifier ( private, public etc.), you can discover the name and types of parameters in a methods signature.

If you have used the class browser , then you can get an idea of the sort of information one can retrieve using reflection.

The System.Reflection namespace contains many classes and methods that provide you with the functionality just described above.

The need for Reflection

While there are many reasons to use reflection, let me give you a real life example. Say you are building a custom component in .NET that requires a XML file to be used along with your components DLL. You can store the XML file in the /bin directory with your DLL, but how will you access the XML file without knowing the correct path to the /bin directory? I'm sure this is a rhetorical question if you have understand even 10% of what I have wrote so far, but I'll tell you anyways: r e f l e c t i o n. I've created a sample code snippet for you that does just this. Keep reading…

Example usage of Reflection

Here's a neat little trick to find out the calling assemblies path.

// using System.IO;
// using System.Reflection;
public static string GetApplicationDirectory
{
 get 
 {
  return Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "");
 }
}
You can also output the executing assembly information during runtime:
<%@ Page language="c#" %>
<%@ Import Namespace="System.Reflection"%>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
 Assembly SampleAssembly;
 // Display the name of the assembly currently executing
 Response.Write("GetExecutingAssembly=" + Assembly.GetExecutingAssembly().FullName);
}
</script>
<html>
 <head>
  <title>CSharpFriend.com - ASP.NET Sample - reflection</title>
 </head>
 <body>
 </body>
</html>

I hope you have understood some basic ideas of what reflection is and what information it can provide you with during runtime.

Enjoy!


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  Reflextion
Current Lesson:
Reflection Explained
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Web.Config Template Reference [next Lesson]  XML In ASP.NET - The XmlTextReader


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
garyweik1 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center