Introduction
In .NET, reflection is the means by which you can discover information about an object at runtime. You might have used the Intermediate Language Disassembler, IlDasm.exe, to look at the information in an assembly. By using reflection, you can obtain the same information from 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:
clsClass cls = new clsClass();
Type t = cls.GetType();
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:
Type t = Type.GetType ("clsClass");
Yet another method of obtaining a Type object is to use the typeof operator, passing the name of the class as a parameter:
Type t = typeof (clsClass);
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
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); } } }
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.
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); } }
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
|
No responses found. Be the first to respond and make money from revenue sharing program.
|