System.Type


The abstract class Type, defined in the System namespace, is an abstraction of a .NET CLR type. Every .NET type, be it a .NET-provided type (from value types such as integers and enums to classes and interfaces) or a developer-defined type, has a corresponding unique Type value.

The ability to uniquely identify a type by means of a Type object is analogous to the COM idea of identifying the type of a component by means of its unique CLSID.


The canonical base class of any .NET type is System.Object. Object (or just object in C#) has built-in support for retrieving the Type associated with any object by calling its GetType( ) method:

     public class Object     {        public Type GetType(  );        //Other methods     }

Having GetType( ) present in object allows you to call it on any .NET object:

     public class MyClass     {...}     int number = 0;     MyClass obj = new MyClass(  );     Type type1 = number.GetType(  );     Type type2 = obj.GetType(  );

Different instances of the same type must return the same Type value:

     int number1 = 1;     int number2 = 2;     Type type1 = number1.GetType(  );     Type type2 = number2.GetType(  );     Debug.Assert(type1 == type2);

The typeof operator allows you to retrieve the Type associated with a type directly, without instantiating an object of that type:

     Type type1 = typeof(int);     Type type2 = typeof(MyClass);

Type is your gateway to obtaining the metadata associated with a given type. To start, Type.ToString( ) returns the type's name:

     Type type = typeof(MyClass);     string name = type.ToString(  );     Debug.Assert(name == "MyClass");

But Type has a lot more to offer. It has more than 100 methods and properties that you can use to obtain metadata about the type. For example, the GetMethods( ) method returns an array of MethodInfo objects describing all the public methods of the type. GetMethods( ) is defined as:

     public MethodInfo[] GetMethods(  );

Example C-1 demonstrates using GetMethods( ) to trace to the output window all the public methods of the class MyClass.

Example C-1. Using Type.GetMethods( ) to reflect a type's public methods
 using System.Reflection; public class MyClass {    public MyClass(  )    {}    public void Method1(  )    {}    public static void Method2(  )    {}    protected void Method3(  )    {}    private void Method4(  )    {} } //Client code: Type type = typeof(MyClass); MethodInfo[] methodInfoArray = type.GetMethods(  ); //Trace all the public methods foreach(MethodInfo methodInfo in methodInfoArray) {    Trace.WriteLine(methodInfo.Name); } //Output: GetHashCode Equals ToString Method1 Method2 GetType 

This example demonstrates a few other key points. GetMethods( ) returns all the public methods of a type (instance or static), including those defined in its base class(es). In its output, Example C-1 lists four public methods of object that are not part of the MyClass definition.

GetMethods( ) doesn't return constructors. If you aren't satisfied with this behavior, you can use another version of GetMethods( ) that accepts a parameter telling it how to bind to the type:

     public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);

The BindingFlags enumeration is a bit-mask enumeration that lets you specify whether to return only instance methods, only static methods, only nonpublic methods, methods defined only in this type (i.e., not inherited), and so on. Once you have obtained a MethodInfo object about a method, you can invoke it, even if it's a protected or private method. Example C-2 demonstrates how to invoke all the private methods of the MyClass class using the late-binding Invoke( ) method of the MethodInfo object. The Invoke( ) method is defined as:

     public object Invoke(object obj, object[] parameters);

Late-binding invocation breaks every rule of encapsulation and type safety, because the compiler lets you invoke private methods and pass in as parameters anything you want. Late binding is available for esoteric cases and for tool developers. In general, avoid reflection-based late-binding invocation.


Example C-2 starts by obtaining a Type object from an instance (unlike Example C-1, which uses the class itself). Then it calls GetMethods( ), requesting back only nonpublic instance methods. Note that to invoke only private methods, further filtering is required after calling GetMethods( ), because GetMethods( ) returns both protected and private methods. The additional filtering is done by checking the IsPrivate property of the MethodInfo object. In Example C-2, only Method4( ) is invoked. Invoke( ) accepts the object to invoke the method on, and an array of objects as parameters for the method to invoke. In this example, Method4( ) has no parameters, so a null is passed in to Invoke( ).

Example C-2. Invoking only the private methods of an object using reflection
 using System.Reflection; public class MyClass {    public MyClass(  )    {...}    public void Method1(  )    {...}    public void Method2(  )    {...}    public static void Method5(  )    {...}    protected void Method3(  )    {...}    private void Method4(  )    {...} } //Client code MyClass obj = new MyClass(  ); Type type = typeof(MyClass); MethodInfo[] methodInfoArray; methodInfoArray = type.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance); //Invoke private methods only foreach(MethodInfo methodInfo in methodInfoArray) {    if(methodInfo.IsPrivate)    {       methodInfo.Invoke(obj,null);    } }

Use of the late-binding Invoke( ) method is similar to the old COM automation way of using IDispatch::Invoke( ) to invoke calls on objects.


Type offers numerous other methods in addition to GetMethods( ). GetMethod( ) returns a MethodInfo object about one particular method. GetConstructors( ) and GetConstructor( ) return ConstructorInfo objects about the type's constructors. GetMember( ) and GetMembers( ) return information about the type's members. GetEvent( ) and GetEvents( ) return information about the events the type supports, and so on. The general form of retrieving the reflection information is Get<Element Name>. Type also offers many properties dealing with what kind of type is reflected: if it's a class or an interface, what its base class type is, and so on. For example:

     Type type = typeof(MyClass);     Debug.Assert(type.IsClass);

See the MSDN Library for a complete listing of the Type members.



Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

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