Introspecting Types Using Reflection


Introspecting Types Using Reflection

The System.Reflection namespaces provide classes for introspecting currently loaded classes and their associated methods and fields. Also, this namespace provides a dynamic way of creating instances of a particular class. The Reflection namespace is particularly useful for tool developers. For instance, if you were to build your own Visual C# or Visual Basic.NET code editor and would like to enable IntelliSense autocomplete functionality for classes and methods , you would use the Reflection class library to achieve that.

Introspecting Classes

In this section, you'll take a quick look at the System.Reflection.Type class, which provides methods for introspecting class definitions. For instance, Listing 4.9 prints out the various members (including fields and methods) for the hks.Customer class.

Listing 4.9 Introspecting Classes
 using System; using System.Reflection; namespace hks {  public class UseReflection  {    public static void Main()    {       Customer c = new Customer();       c.Name = "Hitesh Seth";       c.Address = "111 Street, City, NJ 12345";       c.Id = "HKS-001";       Console.WriteLine(c);       Type ct = Type.GetType("hks.Customer");       Console.WriteLine(ct.FullName+" : "+ct.BaseType);       MemberInfo[] members = ct.GetMembers();       foreach (MemberInfo member in members)       {          Console.WriteLine("-->"+member);       }    }  }  class Customer  {    public String Name, Address, Id;    public override String ToString()    {       return "<Customer Name=\""+Name+"\" Address=\""+Address+"\" Id=\""+Id+"\"/>";    }    public String GetName()    {       return Name;    }    public String GetAddress()    {       return Address;    }  } } 

If you compile and run the preceding program, you should get an output similar to the following:

 
 <Customer Name="Hitesh Seth" Address="111 Street, City, NJ 12345" Id="HKS-001"/> hks.Customer : System.Object -->System.String Name -->System.String Address -->System.String Id -->Int32 GetHashCode() -->Boolean Equals(System.Object) -->System.String ToString() -->System.String GetName() -->System.String GetAddress() -->System.Type GetType() -->Void .ctor() 

You can quickly generalize this program to give you the member list of any loaded .NET type (Listing 4.10).

Listing 4.10 Introspecting Classes, Version 2
 using System; using System.Reflection; public class TypeInfo {    public static void Main(String[] args)    {       Type ct = Type.GetType(args[0]);       Console.WriteLine(ct.FullName+" : "+ct.BaseType);       MemberInfo[] members = ct.GetMembers();       foreach (MemberInfo member in members)       {          Console.WriteLine("-->"+member);       }    } } 

For instance, running TypeInfo System.Object provides the following output:

 
 E:\hks\DotNet\Chapter 4>TypeInfo System.Object System.Object : -->Int32 GetHashCode() -->Boolean Equals(System.Object) -->System.String ToString() -->Boolean Equals(System.Object, System.Object) -->Boolean ReferenceEquals(System.Object, System.Object) -->System.Type GetType() -->Void .ctor() 

Dynamically Creating Types

Apart from getting metadata information about loaded types and their associated members, the Reflection class library provides a mechanism for dynamically instantiating objects of a particular class and invoking underlying methods. For instance, Listing 4.11 creates an instance of type "hks.HelloWorld" and then invokes the SayHello method with a string parameter on the instance.

Listing 4.11 Dynamically Creating Types
 using System; using System.Reflection; namespace hks {  public class UseReflection2  {    public static void Main()    {       Type ct = Type.GetType("hks.HelloWorld");       Object obj = Activator.CreateInstance(ct);       MethodInfo mi = ct.GetMethod("SayHello");       mi.Invoke(obj,new Object[] {"Hitesh"});    }  }  class HelloWorld  {    public void SayHello(String name)    {       Console.WriteLine("Hello "+name+" to a Dynamic World!");    }  } } 

Running the preceding program should yield the following output:

 
 E:\hks\DotNet\Chapter 4>UseReflection2 Hello Hitesh to a Dynamic World! 


Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net