成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何在 C# 中使用 反射

開發(fā) 后端
C# 中的 反射 常用于在程序的運行時獲取 類型 的元數(shù)據(jù),可獲取的信息包括已加載到進程中的 程序集 和 類型 信息,它和 C++ 中的 RTTI(Runtime Type Information) 的作用是差不多的。

[[361289]]

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)讀書」,作者碼農(nóng)讀書。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)讀書公眾號。

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 類。

  1. public class Customer 
  2.     { 
  3.         public int Id { get; set; } 
  4.  
  5.         public string FirstName { get; set; } 
  6.  
  7.         public string LastName { get; set; } 
  8.  
  9.         public string Address { get; set; } 
  10.     } 

下面的代碼片段展示了如何通過 反射 來獲取 Customer 的類名以及 Customer 的所屬命名空間。

  1. class Program 
  2.    { 
  3.        static void Main(string[] args) 
  4.        { 
  5.            Type type = typeof(Customer); 
  6.  
  7.            Console.WriteLine("Class: " + type.Name); 
  8.            Console.WriteLine("Namespace: " + type.Namespace); 
  9.        } 
  10.    } 

再看一個例子,如何通過反射獲取 Customer 下的所有屬性,并且將屬性名字全部展示在控制臺上,如下代碼所示:

  1. static void Main(string[] args) 
  2.         { 
  3.             Type type = typeof(Customer); 
  4.  
  5.             PropertyInfo[] propertyInfo = type.GetProperties(); 
  6.  
  7.             Console.WriteLine("The list of properties of the Customer class are:--"); 
  8.  
  9.             foreach (PropertyInfo pInfo in propertyInfo) 
  10.             { 
  11.                 Console.WriteLine(pInfo.Name); 
  12.             } 
  13.         } 

值得注意的是,typeof(Customer).GetProperties() 默認(rèn)只能獲取 標(biāo)記為 public 的屬性集合,對應(yīng)著 Customer 類下的四個公開屬性。

接下來再來看看如何通過 反射 獲取類型下的 構(gòu)造函數(shù) 和 公共方法 的元數(shù)據(jù)信息,這里還是繼續(xù)使用 Customer 類,在類中新增一個 構(gòu)造函數(shù) 和一個 Validate 方法,此方法用于校驗入?yún)⒌暮戏ㄐ裕旅婢褪切薷暮蟮?Customer 類。

  1. public class Customer 
  2.     { 
  3.         public int Id { get; set; } 
  4.  
  5.         public string FirstName { get; set; } 
  6.  
  7.         public string LastName { get; set; } 
  8.  
  9.         public string Address { get; set; } 
  10.  
  11.         public Customer() { } 
  12.  
  13.         public bool Validate(Customer customerObj) 
  14.         { 
  15.             //Code to validate the customer object 
  16.             return true
  17.         } 
  18.     } 

然后再來看看通過 反射 來獲取 Customer 下所有定義的構(gòu)造函數(shù),不過這里只定義了一個構(gòu)造函數(shù),因此只能列出一個。

  1. class Program 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             Type type = typeof(Customer); 
  6.  
  7.             ConstructorInfo[] constructorInfo = type.GetConstructors(); 
  8.  
  9.             Console.WriteLine("The Customer class contains the following Constructors:--"); 
  10.  
  11.             foreach (ConstructorInfo c in constructorInfo) 
  12.             { 
  13.                 Console.WriteLine(c); 
  14.             } 
  15.         } 
  16.     } 

同樣也要注意,默認(rèn)情況下 GetConstructors() 方法只能獲取 Customer 的所有標(biāo)記為 public 的構(gòu)造函數(shù)。

接下來看看如何展示 Customer 中的所有 public 方法,因為該類中只定義了一個 public 方法,所以控制臺上也應(yīng)該只會展示一個,如下代碼僅供參考。

  1. static void Main(string[] args) 
  2.        { 
  3.            Type type = typeof(Customer); 
  4.  
  5.            MethodInfo[] methodInfo = type.GetMethods(); 
  6.  
  7.            Console.WriteLine("The methods of the Customer class are:--"); 
  8.  
  9.            foreach (MethodInfo temp in methodInfo) 
  10.            { 
  11.                Console.WriteLine(temp.Name); 
  12.            } 
  13.  
  14.            Console.Read(); 
  15.        } 

是不是很驚訝,剛才還說是一個方法,居然多了好幾個,要知道多的那幾個方法,來自于兩方面。

  • 從 object 類型繼承下來的公共方法

  • 編譯器自動生成的屬性方法

如果方法上面標(biāo)記了 Attribute, 還可以通過 GetCustomAttributes 方法來獲取,參考代碼如下:

  1. static void Main(string[] args) 
  2.         { 
  3.             foreach (MethodInfo temp in methodInfo) 
  4.             { 
  5.                 foreach (Attribute attribute in temp.GetCustomAttributes(true)) 
  6.                 { 
  7.                     //Write your usual code here 
  8.                 } 
  9.             } 
  10.         } 

相信在你的應(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

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)讀書
相關(guān)推薦

2021-03-07 16:37:52

C#應(yīng)用程序

2021-02-01 12:36:59

C# Channels存儲

2021-01-19 05:30:55

C# 8異步流IEnumerable

2021-01-18 05:18:18

C# 8模式C# 7

2021-01-22 05:53:08

C# IndexRange

2021-01-28 05:14:40

C#接口簽名

2009-08-04 10:29:06

在C#中使用存儲過程

2021-11-25 00:04:16

C# 插值字符串

2018-08-03 08:37:31

設(shè)計模式IT項目GDPR

2021-03-15 08:18:23

C#反射模塊

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2021-06-09 09:36:18

DjangoElasticSearLinux

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2024-01-18 08:37:33

socketasyncio線程

2011-08-10 09:31:41

Hibernateunion

2019-09-16 19:00:48

Linux變量

2014-07-02 09:47:06

SwiftCocoaPods

2020-11-30 11:55:07

Docker命令Linux
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 国产精品久久久久久久久久久免费看 | 国产免费xxx | 高清久久久 | 亚洲一区 中文字幕 | 精品国产一区二区久久 | 亚洲福利在线观看 | 亚洲精品一二区 | 蜜桃久久 | 免费观看一级毛片 | av高清 | 欧美一区二区三区大片 | 人人爽人人爽 | 国产精品久久久久久久午夜片 | 亚洲欧美久久 | 日韩久久久久久久久久久 | 男人天堂手机在线视频 | 在线免费观看视频你懂的 | 欧美成人一区二区 | 欧美人妇做爰xxxⅹ性高电影 | 精品一区二区不卡 | 国产精品欧美一区喷水 | 国产日韩久久久久69影院 | 久久精品91久久久久久再现 | 精品九九 | 福利片一区二区 | 亚洲aⅴ | 波多野结衣二区 | 日韩成人免费视频 | 天天搞天天操 | 久久久涩 | 麻豆精品国产91久久久久久 | 国产一区二区免费 | 自拍偷拍一区二区三区 | 亚洲精品大全 | 精品91久久 | 一区二区三区回区在观看免费视频 | 免费国产一区 | 中文精品一区二区 | 在线亚洲免费视频 | 欧洲精品一区 | 91久久夜色 |