Type.TypeHandle Type.TypeHandle Type.TypeHandle Type.TypeHandle Property


取得目前 Type 的控制代碼。Gets the handle for the current Type.

 virtual property RuntimeTypeHandle TypeHandle { RuntimeTypeHandle get(); };
public virtual RuntimeTypeHandle TypeHandle { get; }
member this.TypeHandle : RuntimeTypeHandle
Public Overridable ReadOnly Property TypeHandle As RuntimeTypeHandle


目前 Type 的控制代碼。The handle for the current Type.



.NET Compact Framework 目前不支援這個屬性。The .NET Compact Framework does not currently support this property.


下列範例會傳回對應的型別控制代碼,並將控制代碼傳遞取得控制代碼的類型,並顯示它的方法。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 namespace System;
using namespace System::Reflection;
ref class MyClass
   int myField;

void DisplayTypeHandle( RuntimeTypeHandle myTypeHandle )
   // Get the type from the handle.
   Type^ myType = Type::GetTypeFromHandle( myTypeHandle );
   // Display the type.
   Console::WriteLine( "\nDisplaying the type from the handle:\n" );
   Console::WriteLine( "The type is {0}.", myType );

int main()
      MyClass^ myClass = gcnew MyClass;
      // Get the type of MyClass.
      Type^ myClassType = myClass->GetType();
      // Get the runtime handle of MyClass.
      RuntimeTypeHandle myClassHandle = myClassType->TypeHandle;
      DisplayTypeHandle( myClassHandle );
   catch ( Exception^ e ) 
      Console::WriteLine( "Exception: {0}", e->Message );


using System;
using System.Reflection;
class MyClass
    public int myField = 10;

class Type_TypeHandle
    public static void Main()
            MyClass myClass = new MyClass();

            // Get the type of MyClass.
            Type myClassType = myClass.GetType();

            // Get the runtime handle of MyClass.
            RuntimeTypeHandle myClassHandle = myClassType.TypeHandle;
        catch(Exception e)
            Console.WriteLine("Exception: {0}", e.Message );

    public static void DisplayTypeHandle(RuntimeTypeHandle myTypeHandle)
        // Get the type from the handle.
        Type myType = Type.GetTypeFromHandle(myTypeHandle);      
        // Display the type.
        Console.WriteLine("\nDisplaying the type from the handle:\n");
        Console.WriteLine("The type is {0}.", myType.ToString());
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Class [MyClass]
    Public myField As Integer = 10
End Class '[MyClass]
Class Type_TypeHandle
    Public Shared Sub Main()
            Dim [myClass] As New [MyClass]()

            ' Get the type of MyClass.
            Dim myClassType As Type = [myClass].GetType()

            ' Get the runtime handle of MyClass.
            Dim myClassHandle As RuntimeTypeHandle = myClassType.TypeHandle

        Catch e As Exception
            Console.WriteLine("Exception: {0}", e.Message.ToString())
        End Try
    End Sub 'Main

    Public Shared Sub DisplayTypeHandle(ByVal myTypeHandle As RuntimeTypeHandle)
        ' Get the type from the handle.
        Dim myType As Type = Type.GetTypeFromHandle(myTypeHandle)
        ' Display the type.
        Console.WriteLine(ControlChars.NewLine + "Displaying the type from the handle:" + ControlChars.NewLine)
        Console.WriteLine("The type is {0}.", myType.ToString())
    End Sub 'DisplayTypeHandle
End Class 'Type_TypeHandle


TypeHandle 封裝代表類型的內部資料結構的指標。TypeHandle encapsulates a pointer to an internal data structure that represents the type. 這個控制代碼的處理序存留期間是唯一的。This handle is unique during the process lifetime. 控制代碼無效,只能在取得應用程式定義域中。The handle is valid only in the application domain in which it was obtained.