Type.GetTypeHandle 方法

获取指定对象的 Type 的句柄。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Function GetTypeHandle ( _
    o As Object _
) As RuntimeTypeHandle
用法
Dim o As Object
Dim returnValue As RuntimeTypeHandle

returnValue = Type.GetTypeHandle(o)
public static RuntimeTypeHandle GetTypeHandle (
    Object o
)
public:
static RuntimeTypeHandle GetTypeHandle (
    Object^ o
)
public static RuntimeTypeHandle GetTypeHandle (
    Object o
)
public static function GetTypeHandle (
    o : Object
) : RuntimeTypeHandle

参数

  • o
    要获取类型句柄的 Object

返回值

指定 ObjectType 的句柄。

备注

这些句柄仅在获取它们的应用程序域中有效。

示例

下面的示例定义类 MyClass1,获取它的实例,并检索该对象的运行时句柄。

Imports System
Imports System.Reflection

 _
Public Class MyClass1
   Private x As Integer = 0
   
   Public Function MyMethod() As Integer
      Return x
   End Function 'MyMethod
End Class 'MyClass1
 _
Public Class MyClass2
   
   Public Shared Sub Main()
      Dim myClass1 As New MyClass1()
      
      ' Get the RuntimeTypeHandle from an object.
      Dim myRTHFromObject As RuntimeTypeHandle = Type.GetTypeHandle(myClass1)
      ' Get the RuntimeTypeHandle from a type.
      Dim myRTHFromType As RuntimeTypeHandle = GetType(MyClass1).TypeHandle
      
      Console.WriteLine("myRTHFromObject.Value: {0}", myRTHFromObject.Value)
      Console.WriteLine("myRTHFromObject.Type: {0}", myRTHFromObject.GetType())
      Console.WriteLine("myRTHFromType.Value: {0}", myRTHFromType.Value)
      Console.WriteLine("myRTHFromType.Type: {0}", myRTHFromType.GetType())
   End Sub 'Main 
End Class 'MyClass2
using System;
using System.Reflection;
public class MyClass1
{
    private int x=0;
    public int MyMethod()
    {
        return x;
    }
}
public class MyClass2
{
    public static void Main()
    {
        MyClass1 myClass1 = new MyClass1();

        // Get the RuntimeTypeHandle from an object.
        RuntimeTypeHandle myRTHFromObject = Type.GetTypeHandle(myClass1);
        // Get the RuntimeTypeHandle from a type.
        RuntimeTypeHandle myRTHFromType = typeof(MyClass1).TypeHandle;

        Console.WriteLine("myRTHFromObject.Value: {0}", myRTHFromObject.Value);
        Console.WriteLine("myRTHFromObject.Type: {0}", myRTHFromObject.GetType());
        Console.WriteLine("myRTHFromType.Value: {0}", myRTHFromType.Value);
        Console.WriteLine("myRTHFromType.Type: {0}", myRTHFromType.GetType());

    }
}
using namespace System;
using namespace System::Reflection;
public ref class MyClass1
{
private:
   int x;

public:
   int MyMethod()
   {
      return x;
   }
};

int main()
{
   MyClass1^ myClass1 = gcnew MyClass1;
   
   // Get the RuntimeTypeHandle from an object.
   RuntimeTypeHandle myRTHFromObject = Type::GetTypeHandle( myClass1 );
   
   // Get the RuntimeTypeHandle from a type.
   RuntimeTypeHandle myRTHFromType = MyClass1::typeid->TypeHandle;
   Console::WriteLine( "myRTHFromObject.Value: {0}", myRTHFromObject.Value );
   Console::WriteLine( "myRTHFromObject.Type: {0}", myRTHFromObject.GetType() );
   Console::WriteLine( "myRTHFromType.Value: {0}", myRTHFromType.Value );
   Console::WriteLine( "myRTHFromType.Type: {0}", myRTHFromType.GetType() );
}
import System.*;
import System.Reflection.*;

public class MyClass1
{
    private int x = 0;

    public int MyMethod()
    {
        return x;
    } //MyMethod
} //MyClass1

public class MyClass2
{
    public static void main(String[] args)
    {
        MyClass1 myClass1 = new MyClass1();
        // Get the RuntimeTypeHandle from an object.
        RuntimeTypeHandle myRTHFromObject = Type.GetTypeHandle(myClass1);
        // Get the RuntimeTypeHandle from a type.
        RuntimeTypeHandle myRTHFromType = (MyClass1.class.ToType()).
            get_TypeHandle();

        Console.WriteLine("myRTHFromObject.Value: {0}", myRTHFromObject.
            get_Value());
        Console.WriteLine("myRTHFromObject.Type: {0}", myRTHFromObject.
            GetType());
        Console.WriteLine("myRTHFromType.Value: {0}", myRTHFromType.
            get_Value());
        Console.WriteLine("myRTHFromType.Type: {0}", myRTHFromType.
            GetType());
    }//main 
} //MyClass2

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Type 类
Type 成员
System 命名空间
RuntimeTypeHandle 结构
TypeHandle
GetTypeFromHandle