C Sharp

typeof

Reflection is the ability to retrieve type information at run time. This information includes type names, class names, and structure members. Integral to this ability in the .NET Framework is a class called System.Type. This class is the root of all reflection operators, and it can be obtained by using the typeof operator. We won't be getting into reflection in great detail here-that's a task reserved for Chapter 16, "Querying Metadata with Reflection"-but here's a simple example to illustrate how easy it is to use the typeof operator to retrieve almost any information you want about a type or object at run time: -

using System;
using System.Reflection;
public class Apple
{
    public int nSeeds;
    public void Ripen()
    {
    }
}
public class TypeOfApp
{
    public static void Main()
    {
        Type t = typeof(Apple);
        string className = t.ToString();
        Console.WriteLine("\nInformation about the class {0}",
            className);
        Console.WriteLine("\n{0} methods", className);
        Console.WriteLine("-----------------------------");
        MethodInfo[] methods = t.GetMethods();
        foreach (MethodInfo method in methods)
        {
            Console.WriteLine(method.ToString());
        }
        Console.WriteLine("\nAll {0} members", className);
        Console.WriteLine("-----------------------------");
        MemberInfo[] allMembers = t.GetMembers();
        foreach (MemberInfo member in allMembers)
        {
            Console.WriteLine(member.ToString());
        }
    }
}

The program contains a class called Apple that has only two members: a field named nSeeds and a method named Ripen. The first thing I do is to use the typeof operator and the class name to yield a System.Type object, which I then store in a variable named t. From that point on, I can use the System.Type methods to obtain all the Apple class methods and members. These tasks are performed using the GetMethods and GetMembers methods, respectively. The results of these method calls are printed to standard output, as shown here: -

Information about the class Apple
Apple methods
-----------------------------
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void Ripen()
System.Type GetType()
All Apple members
-----------------------------
Int32 nSeeds
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void Ripen()
System.Type GetType()
Void .ctor()

Before we move on, there are two more points I'd like to make. First, notice that the class's inherited members are also listed in the output. Because I did not explicitly derive the class from another class, we know that all the members not defined in the Apple class are inherited from the implicit base class, System.Object. Second, note that you can also use the GetType method to retrieve the System.Type object. This is a method inherited from System.Object that enables you to work with objects, as opposed to classes. The two code snippets at the top of the next page could be used interchangeably to retrieve a System.Type object.

// Retrieve the System.Type object from the class definition.
Type t1 = typeof(Apple);
// Retrieve the System.Type object from the object.
Apple apple = new Apple();
Type t2 = apple.GetType();

by BrainBellupdated
Advertisement: