Reflection API

Reflection represents an extremely powerful mechanism for working with .NET components. Through the use of reflection, .NET components can be dynamically loaded and their contents discovered at runtime. Metadata is the heart of this functionality and, as such, Metadata can be thought of as a TypeLib on steroids.

The .NET CLR provides for strong runtime type information and provides a System.Type class. After a .NET component is loaded, it is then possible to obtain the type or types contained within the given assembly. To demonstrate the power of Metadata and the Reflection API, create a text file named employee.cs and copy Listing 1.3.4 into it. To compile the listing, issue the following command:

csc /t:library employee.cs

This will build the employee.dll assembly.

Listing 1.3.4. The Employee Class
 1: //File        :employee.cs
 2: //Author      :Richard L. Weeks
 3: //Purpose     :Create a .NET component and use the reflection
 4: //             API to view the component
 5:
 6: using System;
 7:
 8: namespace stingray {
 9:
10:
11:      //Create a basic class
12:      public class Employee {
13:
14:            //private data members
15:            private string   m_FirstName;
16:            private string   m_LastName;
17:            private string   m_Title;        //Job Title
18:
19:            //Public Properties
20:            public string FirstName {
21:                   get {  return m_FirstName; }
22:                   set {  m_FirstName = value; }
23:            }
24:
25:            public string LastName {
26:                   get {  return m_LastName; }
27:                   set {  m_LastName = value; }
28:            }
29:
30:            public string Title {
31:                   get {  return m_Title; }
32:                   set {  m_Title = value; }
33:            }
34:
35:            //Public Methods
36:            public void ShowData( ) {
37:                object[] args = {  m_LastName, m_FirstName, m_Title } ;
38:
39:                Console.WriteLine("******************** n");
40:                Console.WriteLine("Employee : { 0} , { 1}  nTitle     :{ 2}  n", args);
41:                Console.WriteLine("******************** n");
42:            }
43:
44:            //Private methods
45:            private void Update( ) {
46:                 //TODO: Update Employee information
47:            }
48:     }
49: }
50:

The Employee class resides in the stingray namespace and contains three private data members, corresponding public properties, and two methods. Using the Reflection API, it is possible to dynamically load the employee.dll at runtime. After the assembly has been loaded, all types contained within the assembly can be queried and their Metadata accessed. Listing 1.3.5 makes use of the Reflection API to peek into a .NET component.

Listing 1.3.5. Metaview Source Listing
 1: //File        :metaview.cs
 2: //Author      :Richard L. Weeks
 3: //Purpose     :Use the Managed Reflection API
 4: //             to inspect a .NET component
 5:
 6:
 7: using System;
 8: using System.Reflection;
 9:
10:
11:
12: public class ClsView {
13:
14:
15:     public static void Main( String[] args ) {
16:
17:       try {
18:
19:            //Load the assembly
20:            Assembly asm = Assembly.LoadFrom( args[0] );
21:
22:            //Get the Types contained within the Assembly
23:            System.Type[] Types = asm.GetTypes( );
24:
25:            //Display basic information about each type
26:            foreach( Type T in Types ) {
27:               Console.WriteLine("Type { 0} ", T.FullName);
28:               DisplayFields( T );
29:               DisplayProperties( T );
30:               DisplayMethods( T );
31:               Console.WriteLine("");
32:            }
33:
34:       }  catch( Exception ex ) {
35:            Console.WriteLine( ex );
36:       }
37:
38:     }
39:
40:
41:     public static void DisplayFields( Type T ) {
42:       Console.WriteLine("*****[ DisplayFields ]*****");
43:       FieldInfo[] Fields = T.GetFields( System.Reflection.BindingFlags.Instance |
44:  System.Reflection.BindingFlags.Public |
45:  System.Reflection.BindingFlags.NonPublic );
46:
47:        foreach( FieldInfo F in Fields )
48:           Console.WriteLine( "FieldName = { 0} ", F.Name );
49:     }
50:
51:     public static void DisplayProperties( Type T ) {
52:        Console.WriteLine("*****[ Display Properties ]*****");
53: System.Reflection.PropertyInfo[] Properties = T.GetProperties( System.Reflection
.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection
.BindingFlags.NonPublic );
54:
55:         foreach( PropertyInfo pi in Properties )
56:             Console.WriteLine( pi.Name );
57:     }
58:
59:     public static void DisplayMethods( Type T ) {
60:         Console.WriteLine("*****[ Display Methods ]*****");
61:         System.Reflection.MethodInfo[] Methods = T.GetMethods( System.Reflection
.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection
.BindingFlags.NonPublic );
62:
63:         foreach( MethodInfo mi in Methods )
64:            Console.WriteLine( mi.Name );
65:     }
66: }

Listing 1.3.5 makes use of the most basic Reflection APIs to detail information about each field, property, and method with the loaded employee assembly. Although the C# language has not been covered yet, most of the code should speak for itself. I encourage you to implement a robust version of Metaview, complete with a Windows Forms GUI. Such a project will no doubt increase your knowledge of .NET and the rich set of features it provides.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.124