当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Type.GetTypeFromHandle()用法及代码示例


Type.GetTypeFromHandle()方法用于获取指定类型句柄引用的类型。

用法: public static Type GetTypeFromHandle (RuntimeTypeHandle handle);
Here, it takes the object which refers to the type.

返回值:此方法返回指定的RuntimeTypeHandle引用的类型;如果handle的Value属性为null,则返回null。


以下示例程序旨在说明Type.GetTypeFromHandle()方法的使用:

示例1:

// C# program to demonstrate the 
// Type.GetTypeFromHandle() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // creating and initializing object Type 
        Type type = typeof(int); 
  
        // creating object of RuntimeTypeHandle  
        // and getting instance of type 
        RuntimeTypeHandle handle = Type.GetTypeHandle(type); 
  
        // Getting the type referenced by 
        // the specified RuntimeTypeHandle, 
        // using GetTypeFromHandle() Method 
        Type outtype = Type.GetTypeFromHandle(handle); 
  
        // Display the Result 
        Console.WriteLine("Type referenced is {0}", outtype); 
        Console.WriteLine("Attributes are: " + outtype.Attributes); 
    } 
}
输出:
Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit

示例2:

// C# program to demonstrate the 
// Type.GetTypeFromHandle() Method 
using System; 
using System.Globalization; 
using System.Reflection; 
  
// Defining Empty struct 
struct Empty {} 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        // creating and initializing object Type 
        Type type = typeof(Empty); 
  
        // creating object of RuntimeTypeHandle  
        // and getting instance of type 
        RuntimeTypeHandle handle = Type.GetTypeHandle(type); 
  
        // Getting the type referenced by 
        // the specified RuntimeTypeHandle, 
        // using GetTypeFromHandle() Method 
        Type outtype = Type.GetTypeFromHandle(handle); 
  
        // Display the Result 
        Console.WriteLine("Type referenced is {0}", outtype); 
        Console.WriteLine("Attributes are: " + outtype.Attributes); 
    } 
}
输出:
Type referenced is System.RuntimeType
Attributes are: AutoLayout, AnsiClass, Class, SequentialLayout, Serializable, BeforeFieldInit

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.GetTypeFromHandle() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。