當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。