首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 取得泛型型別 Type 物件:typeof 與 GetType 完整指南

     編輯:關於C語言

前言

在反射與動態程式設計中,取得泛型型別的 Type 物件是基礎操作。本文詳細說明開放型別與封閉建構型別的差異、typeof 與 GetType() 的正確用法,以及常見錯誤與解決方案。

核心概念:開放型別 vs 封閉建構型別

  • 開放型別:GenericClass<T> — 型別參數未指定,無法建立實例,沒有 Type 物件
  • 封閉建構型別:GenericClass<int>、GenericClass<string> — 型別參數已具體化,擁有獨立 Type 物件

typeof 運算子用法

一般類別

Type t = typeof(Simple);  // 直接使用類別名稱

泛型類別:必須提供型別引數

// 正確:提供具體型別引數
Type gtInt = typeof(GenericClass<int>);
Type gtBool = typeof(GenericClass<bool>);
Type gtString = typeof(GenericClass<string>);

// 錯誤:不提供型別引數
// Type gt = typeof(GenericClass);  // CS0305: 型別引數計數錯誤

GetType() 方法用法

實例方法,適用於一般類別與泛型類別實例:

// 一般類別
Simple s = new Simple();
Type t1 = typeof(Simple);
Type t2 = s.GetType();  // 相同

// 泛型類別實例
GenericClass<int> gci = new GenericClass<int>();
Type gt1 = typeof(GenericClass<int>);
Type gt2 = gci.GetType();  // 相同,回傳 GenericClass`1[System.Int32]

常見錯誤與解決方案

錯誤 1:嘗試取得開放型別的 Type

// 編譯錯誤 CS0305
Type gt = typeof(GenericClass);  // 缺少 1 個型別引數

// 正確做法:必須指定型別參數
Type gtInt = typeof(GenericClass<int>);

錯誤 2:混淆 typeof 與 GetType() 使用時機

情境正確用法
編譯期已知型別typeof(SomeType) 或 typeof(Generic<Int>)
執行期從實例取得instance.GetType()
執行期從字串取得Type.GetType("Namespace.Generic`1[[System.Int32]]")

進階:取得泛型型別定義與型別引數

Type closedType = typeof(List<int>);

// 取得開放型別定義
Type openType = closedType.GetGenericTypeDefinition();  // List`1

// 取得型別引數
Type[] args = closedType.GetGenericArguments();  // [System.Int32]

Console.WriteLine(openType);   // System.Collections.Generic.List`1[T]
Console.WriteLine(args[0]);    // System.Int32

實務應用場景

  • 依賴注入容器:註冊 typeof(IRepository<>) 開放型別,解析時建構 typeof(Repository<Customer>)
  • 序列化/反序列化:執行期根據資料決定具體泛型型別
  • 動態建構實例:Activator.CreateInstance(typeof(GenericClass<int>))
  • 屬性/介面檢查:type.GetInterfaces() 檢查泛型介面實作

總結

泛型型別的 Type 物件只存在於「封閉建構型別」(已指定型別參數)。typeof 需在編譯期提供型別引數;GetType() 從實例取得。理解開放/封閉型別區別,是正確使用泛型反射的關鍵。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved