日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在C#中使用反射

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

創(chuàng)新互聯(lián)建站是專業(yè)的尉氏網(wǎng)站建設(shè)公司,尉氏接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行尉氏網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

為了能夠使用反射,需要在項(xiàng)目中引用 System.Reflection 命名空間,在使用反射的開始,你會(huì)獲取一個(gè) Type 類型的對象,從這個(gè)對象上進(jìn)一步獲取 程序集,類型,模塊 等信息,可以通過 反射 動(dòng)態(tài)的生成某個(gè)類型的實(shí)例,甚至還能動(dòng)態(tài)調(diào)用這個(gè)類型上的方法。

在 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.         public string FirstName { get; set; }
  5.         public string LastName { get; set; }
  6.         public string Address { get; set; }
  7.     }

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

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

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

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

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

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

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

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

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

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

接下來看看如何展示 Customer 中的所有 public 方法,因?yàn)樵擃愔兄欢x了一個(gè) public 方法,所以控制臺(tái)上也應(yīng)該只會(huì)展示一個(gè),如下代碼僅供參考。

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

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

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

  • 編譯器自動(dòng)生成的屬性方法

如果方法上面標(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)常會(huì)在 領(lǐng)域?qū)嶓w 上使用各種 Attribute 特性,這時(shí)候就可以通過上面的代碼反射提取 領(lǐng)域?qū)嶓w 中的方法上的Attribute信息,從而根據(jù)提取到的 Attribute 執(zhí)行你的具體業(yè)務(wù)邏輯。

譯文鏈接:https://www.infoworld.com/article/3027240/how-to-work-with-reflection-in-c.html


本文名稱:如何在C#中使用反射
網(wǎng)站路徑:http://www.dlmjj.cn/article/dhjceoe.html