Obtaining a Class s Type


Obtaining a Class's Type

You can obtain a class's type object in several ways. In this section you will learn the two most common ways. Once you have a class's type object you can use a feature called reflection to find out the type's methods , fields, constructors, etc. In Chapter 12, "Attributes," you'll learn how to use the Type object to retrieve information about the attributes in a class.

To obtain a Type object from an object:

If you already have an instance of the class in a variable then you can do the following:

  • Type Type t1 = var.GetType(); where t1 is any variable to hold the reference to the Type object and var is the variable that holds a reference to the object from which you wish to obtain type information ( Figure 7.6 ).

    Figure 7.6 It's easy to get a Type object from an instance of a class. All you have to do is call the GetType method. All classes get this method from System.Object.
     //given the following definition class SuperheroMouse {    public void SaveSomeone    {    } } //use this code to get a type object SuperheroMouse mightyMouse = new SuperheroMouse(); Type t1 = mightyMouse.  GetType()  ; 

To obtain a Type object from a class (without creating an object):

  • Type Type t1 = typeof(classname); where t1 is any variable to hold the reference to the Type object and classname is the name of the class (without quotation marks) from which you wish to obtain type information ( Figure 7.7 ).

    Figure 7.7 Believe it or not, doing this is the same as executing the code in Figure 7.6. Whether you use an instance of the class or the typeof function with the class name, the result is the same Type object. The Type object simply describes the type itself.
     //given the following definition class SuperheroMouse {    public void SaveSomeone    {    } } //use this code to get a type object Type t1 =  typeof(SuperheroMouse)  ; 

graphics/tick.gif Tips

  • The first way to retrieve an object's Type object is by using a function in System.Object called GetType() . Every object derives from System.Object, so all objects have a GetType() function.

  • Why would you want to get an object's Type object? Figure 7.8 shows you an example of using the Type object to create an array. A number of functions that can create objects dynamically at runtime require you to provide the type of object you wish to create using an instance of the Type class.

    Figure 7.8 I know there can only be one Mighty Mouse, but the above code creates an array of five SuperheroMouse elements. It illustrates a generic way of creating arrays: you feed the CreateInstance function the type of each element, and the number of elements, and it creates an array for you at runtime.
     //generic way of creating an array Array arr = Array.CreateInstance (  typeof(SuperheroMouse)  ,5); 
  • When you use the GetType() function, the function reports the type of the object the variable points to, not the type of the variable. For example, your code may read object obj = new Checking(); . This code is legitimate because you can always assign a variable of the parent type to an instance of a derived type. If you were to call obj.GetType() and ask for the Name property you would notice that the type name reported is Checking and not object ( Figure 7.9 ).

    Figure 7.9 At first glance you would think that calling obj.GetType() would return a Type object that describes the object data type. Actually, GetType always reports the type that was instantiated , not the type of the variable.
     class Checking { } object obj =  new Checking()  ; Response.Write(obj.GetType().Name); //outputs: //Checking 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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