次の方法で共有


Type.TypeHandle Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the handle for the current Type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property TypeHandle As RuntimeTypeHandle
public virtual RuntimeTypeHandle TypeHandle { get; }

Property Value

Type: System.RuntimeTypeHandle
The handle for the current Type.

Exceptions

Exception Condition
NotSupportedException

The .NET Compact Framework does not currently support this property.

Remarks

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.

Examples

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.

Imports System.Reflection
Class [MyClass]
   Public myField As Integer = 10
End Class '[MyClass]
Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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(outputBlock, myClassHandle)
      Catch e As Exception
         outputBlock.Text += String.Format("Exception: {0}", e.Message.ToString()) & vbCrLf
      End Try
   End Sub 'Main

   Public Shared Sub DisplayTypeHandle(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myTypeHandle As RuntimeTypeHandle)
      ' Get the type from the handle.
      Dim myType As Type = Type.GetTypeFromHandle(myTypeHandle)
      ' Display the type.
      outputBlock.Text &= ControlChars.NewLine + "Displaying the type from the handle:" + ControlChars.NewLine & vbCrLf
      outputBlock.Text += String.Format("The type is {0}.", myType.ToString()) & vbCrLf
   End Sub 'DisplayTypeHandle
End Class 'Type_TypeHandle
using System;
using System.Reflection;
class MyClass
{
   public int myField = 10;
}

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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(outputBlock, myClassHandle);
      }
      catch (Exception e)
      {
         outputBlock.Text += String.Format("Exception: {0}", e.Message) + "\n";
      }
   }

   public static void DisplayTypeHandle(System.Windows.Controls.TextBlock outputBlock, RuntimeTypeHandle myTypeHandle)
   {
      // Get the type from the handle.
      Type myType = Type.GetTypeFromHandle(myTypeHandle);
      // Display the type.
      outputBlock.Text += "\nDisplaying the type from the handle:\n" + "\n";
      outputBlock.Text += String.Format("The type is {0}.", myType.ToString()) + "\n";
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.