在 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


更新於: 10-12-2019

138 次瀏覽

開啟你的 職業 生涯

透過完成課程獲得認證

開始
廣告