在 C# 中獲取由指定型別控制代碼引用的型別
若要獲取由指定型別控制代碼引用的型別,程式碼如下 −
示例
using System; public class Demo { public static void Main() { Type type1 = typeof(short); RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1); Type type = Type.GetTypeFromHandle(typeHandle); Console.WriteLine("Attributes = " + type.Attributes); } }
輸出
這將生成以下輸出 −
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit
示例
讓我們看另一個示例 −
using System; public class Demo { public static void Main() { Type type1 = typeof(System.Type); RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1); Type type = Type.GetTypeFromHandle(typeHandle); Console.WriteLine("Attributes = " + type.Attributes); } }
輸出
這將生成以下輸出 −
Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit
廣告