C# Type TypeHandle

Description

Type TypeHandle gets the handle for the current Type.

Syntax

Type.TypeHandle has the following syntax.


public virtual RuntimeTypeHandle TypeHandle { get; }

Example

The following example returns the handle of the corresponding type and passes the handle to a method that gets the type from the handle and displays it.


using System;//from  w  ww .ja  v a  2s.c  om
using System.Reflection;
class MyClass
{
    public int myField = 10;
}

class Type_TypeHandle
{
    public static void Main()
    {
        try
        {
            MyClass myClass = new MyClass();
            Type myClassType = myClass.GetType();
            RuntimeTypeHandle myClassHandle = myClassType.TypeHandle;
            DisplayTypeHandle(myClassHandle);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message );
        }
    }

    public static void DisplayTypeHandle(RuntimeTypeHandle myTypeHandle)
    {
        Type myType = Type.GetTypeFromHandle(myTypeHandle);      
        Console.WriteLine("The type is {0}.", myType.ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version