Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Getting Runtime Information Using Reflection
Author: phanindra yerra
Published: 6/29/2006 6:46 AM
Category: C#
Summary: Summary



This article explained how to get object information at runtime using System.Reflections

 

Getting Runtime Information Using Reflection

 

In .NET, reflection is the means by which you can discover information about

an object at runtime. You have used the Intermediate Language Disassembler, IlDasm.exe, to look at the information in an assembly. By using reflection, you can obtain this same information from within a program.

 

Type Class

 

   The key to discovering type information, and to begin exploring reflection, is the Type class, which is a member of the System.Reflection namespace.

The Type class contains methods for extracting information about a given object, including the methods, properties, and fields, along with the interfaces an object supports. The Type class is abstract, so you cannot create an instance of it directly.

 

    In C#, every class or structure that you define ultimately derives from the Object class, and thus inherits a GetType() method. When you call GetType(), the method returns a reference to a Type object.From this object, you can call a rich set of methods to get information about a class or structure.

    Assuming you have a class name clsClass, the following snippet shows how to get a Type object:

 

[code]

 

clsClass cls = new clsClass();

 

Type t = cls.GetType();

 

[/code]

 

The Type class also has a static method, GetType(), that allows you to get a Type object without having to create an instance of a class or structure. You need only pass the method the quoted name of the class:

[code]

 

Type t = Type.GetType ("clsClass");

 

 

[/code]

 

Yet another method of obtaining a Type object is to use the typeof operator, passing the name of the class as a parameter:

[code]

 

Type t = typeof (clsClass);

 

[/code]

 

Once you have the Type object, you can use member methods to extract information

about the class. The following program, GetType.cs, uses a Type object to list the methods,properties, and fields in a class named clsEmployee:

 

 

 GetType.cs -- Demonstrates using Type class to discover information about a class

 

[code]

 

using System;

using System.Reflection;

namespace nsReflection

{

public class clsEmployee

{

 

 

public string FirstName;

public string LastName;

public string ZipCode;

public int EmployeeID;

 

 

public clsEmployee (string First, string Last, string Zip, int ID)

{

FirstName = First;

LastName = Last;

EmployeeID = ID;

ZipCode = Zip;

}

public string Name

{

get {return (FirstName + " " + LastName);}

}

public string Zip

{

get {return (ZipCode);}

}

public int ID

{

get {return (EmployeeID);}

}

static public int CompareByName (object o1, object o2)

{

clsEmployee emp1 = (clsEmployee) o1;

clsEmployee emp2 = (clsEmployee) o2;

return (String.Compare (emp1.LastName, emp2.LastName));

}

static public int CompareByZip (object o1, object o2)

{

clsEmployee emp1 = (clsEmployee) o1;

clsEmployee emp2 = (clsEmployee) o2;

return (String.Compare (emp1.ZipCode, emp2.ZipCode));

}

static public int CompareByID (object o1, object o2)

{

clsEmployee emp1 = (clsEmployee) o1;

clsEmployee emp2 = (clsEmployee) o2;

return (emp1.EmployeeID - emp2.EmployeeID);

}

}

class clsMain

{

static public void Main ()

{

Type t = typeof(clsEmployee);

if (t == null)

{

Console.WriteLine ("t is null");

return;

}

// Show what namespace the class is in

 

Console.WriteLine ("Class clsEmployee is a member of " +

"the {0} namespace", t.Namespace);

 

Console.WriteLine ("\r\nMethods in clsEmployee:");

 

MethodInfo [] methods = t.GetMethods ();

 

// Show the methods in the class

 

foreach (MethodInfo m in methods)

Console.WriteLine ("\t" + m.Name);

 

// Show the properties in the class

 

 

Console.WriteLine ("\r\nProperties in clsEmployee:");

PropertyInfo [] props = t.GetProperties ();

foreach (PropertyInfo p in props)

Console.WriteLine ("\t" + p.Name);

 

// Show the fields in the class

 

Console.WriteLine ("\r\nFields in clsEmployee:");

FieldInfo [] fields = t.GetFields ();

 

foreach (FieldInfo f in fields)

Console.WriteLine ("\t" + f.Name);

}

}

}

 

[/code]

 

Running GetType.cs will list the namespace for the clsEmployee class along with the member methods, properties, and fields. Notice that the methods return only information about members to which you have access. To prove this, in the preceding code, try changing the member fields for the employee’s name to protected or private.

 

You also can use reflection to obtain information about an entire assembly.

The following program, Asy.cs, will load the assembly from the global assembly cache and list information about the assembly, plus information about the structure and class that it contains. To get the assembly information, you use the Assembly class, which is also a member of the System.Reflection namespace. Calling the static member Load will return a reference to the assembly information.

 

 Asy.cs -- Demonstrates using reflection to get information about an assembly.

 

[code]

 

using System;

using System.Reflection;

class clsMain

{

static public void Main ()

{

Assembly asy = null;

try

{

asy = Assembly.Load ("ASMNAME");

}

catch (Exception e)

{

Console.WriteLine (e.Message);

return;

}

Type [] types = asy.GetTypes();

foreach (Type t in types)

ShowTypeInfo (t);

}

static void ShowTypeInfo (Type t)

{

Console.WriteLine ("{0} is a member of the {1} namespace",

t.Name, t.Namespace);

Console.WriteLine ("\r\nMethods in {0}:", t.Name);

 

MethodInfo [] methods = t.GetMethods ();

foreach (MethodInfo m in methods)

 

Console.WriteLine ("\t" + m.Name);

Console.WriteLine ("\r\nProperties in {0}:", t.Name);

 

PropertyInfo [] props = t.GetProperties ();

foreach (PropertyInfo p in props)

Console.WriteLine ("\t" + p.Name);

 

Console.WriteLine ("\r\nFields in {0}:", t.Name);

FieldInfo [] fields = t.GetFields ();

foreach (FieldInfo f in fields)

Console.WriteLine ("\t" + f.Name);

}

}

[/code]

 

When you run Asy.cs, you should see the classes ,structures

member properties, methods, and fields presented in the assembly ASMNAME.

 

Summary

 

   This article explained how to get object information at runtime using System.Reflections


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!?



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
online storage
Color Laser Printer
Gucci sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139