如何在 C# 中使用 反射
C# 中的 反射 常用于在程序的運行時獲取 類型 的元數(shù)據(jù),可獲取的信息包括已加載到進程中的 程序集 和 類型 信息,它和 C++ 中的 RTTI(Runtime Type Information) 的作用是差不多的。
為了能夠使用反射,需要在項目中引用 System.Reflection 命名空間,在使用反射的開始,你會獲取一個 Type 類型的對象,從這個對象上進一步獲取 程序集,類型,模塊 等信息,可以通過 反射 動態(tài)的生成某個類型的實例,甚至還能動態(tài)調(diào)用這個類型上的方法。
在 System.Reflection 命名空間下,定義了如下幾大核心類型。
Assembly
Module
Enum
MethodInfo
ConstructorInfo
MemberInfo
ParameterInfo
Type
FieldInfo
EventInfo
PropertyInfo
現(xiàn)在我們一起研究一下怎么使用,考慮下面定義的 Customer 類。
public?class?Customer{public?int?Id?{?get;?set;?}public?string?FirstName?{?get;?set;?}public?string?LastName?{?get;?set;?}public?string?Address?{?get;?set;?}}下面的代碼片段展示了如何通過 反射 來獲取 Customer 的類名以及 Customer 的所屬命名空間。
class?Program{static?void?Main(string[]?args){Type?type?=?typeof(Customer);Console.WriteLine("Class:?"?+?type.Name);Console.WriteLine("Namespace:?"?+?type.Namespace);}}再看一個例子,如何通過反射獲取 Customer 下的所有屬性,并且將屬性名字全部展示在控制臺上,如下代碼所示:
static?void?Main(string[]?args){Type?type?=?typeof(Customer);PropertyInfo[]?propertyInfo?=?type.GetProperties();Console.WriteLine("The?list?of?properties?of?the?Customer?class?are:--");foreach?(PropertyInfo?pInfo?in?propertyInfo){Console.WriteLine(pInfo.Name);}}值得注意的是,typeof(Customer).GetProperties() 默認只能獲取 標記為 public 的屬性集合,對應(yīng)著 Customer 類下的四個公開屬性。
接下來再來看看如何通過 反射 獲取類型下的 構(gòu)造函數(shù) 和 公共方法 的元數(shù)據(jù)信息,這里還是繼續(xù)使用 Customer 類,在類中新增一個 構(gòu)造函數(shù) 和一個 Validate 方法,此方法用于校驗入?yún)⒌暮戏ㄐ?#xff0c;下面就是修改后的 Customer 類。
public?class?Customer{public?int?Id?{?get;?set;?}public?string?FirstName?{?get;?set;?}public?string?LastName?{?get;?set;?}public?string?Address?{?get;?set;?}public?Customer()?{?}public?bool?Validate(Customer?customerObj){//Code?to?validate?the?customer?objectreturn?true;}}然后再來看看通過 反射 來獲取 Customer 下所有定義的構(gòu)造函數(shù),不過這里只定義了一個構(gòu)造函數(shù),因此只能列出一個。
class?Program{static?void?Main(string[]?args){Type?type?=?typeof(Customer);ConstructorInfo[]?constructorInfo?=?type.GetConstructors();Console.WriteLine("The?Customer?class?contains?the?following?Constructors:--");foreach?(ConstructorInfo?c?in?constructorInfo){Console.WriteLine(c);}}}同樣也要注意,默認情況下 GetConstructors() 方法只能獲取 Customer 的所有標記為 public 的構(gòu)造函數(shù)。
接下來看看如何展示 Customer 中的所有 public 方法,因為該類中只定義了一個 public 方法,所以控制臺上也應(yīng)該只會展示一個,如下代碼僅供參考。
static?void?Main(string[]?args){Type?type?=?typeof(Customer);MethodInfo[]?methodInfo?=?type.GetMethods();Console.WriteLine("The?methods?of?the?Customer?class?are:--");foreach?(MethodInfo?temp?in?methodInfo){Console.WriteLine(temp.Name);}Console.Read();}是不是很驚訝,剛才還說是一個方法,居然多了好幾個,要知道多的那幾個方法,來自于兩方面。
從 object 類型繼承下來的公共方法
編譯器自動生成的屬性方法
如果方法上面標記了 Attribute, 還可以通過 GetCustomAttributes 方法來獲取,參考代碼如下:
static?void?Main(string[]?args){foreach?(MethodInfo?temp?in?methodInfo){foreach?(Attribute?attribute?in?temp.GetCustomAttributes(true)){//Write?your?usual?code?here}}}相信在你的應(yīng)用程序中,經(jīng)常會在 領(lǐng)域?qū)嶓w 上使用各種 Attribute 特性,這時候就可以通過上面的代碼反射提取 領(lǐng)域?qū)嶓w 中的方法上的Attribute信息,從而根據(jù)提取到的 Attribute 執(zhí)行你的具體業(yè)務(wù)邏輯。
譯文鏈接:https://www.infoworld.com/article/3027240/how-to-work-with-reflection-in-c.html
總結(jié)
以上是生活随笔為你收集整理的如何在 C# 中使用 反射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银河麒麟V10入选2020中国十大科技新
- 下一篇: C# 9 新特性——init only