Type.TypeHandle 屬性

定義

取得目前 Type 的控制代碼。

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

屬性值

RuntimeTypeHandle

目前 Type 的控制代碼。

實作

例外狀況

.NET Compact Framework 目前不支援這個屬性。

範例

下列範例會傳回對應類型的控制碼,並將控制碼傳遞給方法,以從控制碼取得型別並加以顯示。

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
   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()
{
   try
   {
      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()
    {
        try
        {
            MyClass myClass = new 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 );
        }
    }

    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.Reflection

Class [MyClass]
    Public myField As Integer = 10
End Class
Class Type_TypeHandle
    Public Shared Sub Main()
        Try
            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

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

    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
End Class

備註

TypeHandle 封裝代表型別的內部資料結構指標。 這個控制碼在進程存留期間是唯一的。 控制碼只在取得它的應用程式域中有效。

適用於

另請參閱