偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

通過(guò)C#反射特性查看自定義特性

開(kāi)發(fā) 后端
本文介紹了如何通過(guò)C#反射特性查看自定義特性。利用反射來(lái)查看自定義特性信息與查看其他信息類似。

利用C#反射來(lái)查看自定義特性信息與查看其他信息類似,首先基于類型(本例中是DemoClass)獲取一個(gè)Type對(duì)象,然后調(diào)用Type對(duì)象的GetCustomAttributes()方法,獲取應(yīng)用于該類型上的特性。當(dāng)指定GetCustomAttributes(Type attributeType, bool inherit) 中的***個(gè)參數(shù)attributeType時(shí),將只返回指定類型的特性,否則將返回全部特性;第二個(gè)參數(shù)指定是否搜索該成員的繼承鏈以查找這些屬性。

C#反射:代碼

  1. class Program {     
  2.     static void Main(string[] args) {     
  3.        Type t = typeof(DemoClass);     
  4.        Console.WriteLine("下面列出應(yīng)用于 {0} 的RecordAttribute屬性:" , t);     
  5.     
  6.        // 獲取所有的RecordAttributes特性     
  7.        object[] records = t.GetCustomAttributes(typeof(RecordAttribute), false);     
  8.     
  9.        foreach (RecordAttribute record in records) {     
  10.            Console.WriteLine("   {0}", record);     
  11.            Console.WriteLine("      類型:{0}", record.RecordType);     
  12.            Console.WriteLine("      作者:{0}", record.Author);     
  13.            Console.WriteLine("      日期:{0}", record.Date.ToShortDateString());     
  14.            if(!String.IsNullOrEmpty(record.Memo)){     
  15.               Console.WriteLine("      備注:{0}",record.Memo);     
  16.            }     
  17.        }     
  18.     }     
  19. }    

輸出為:

下面列出應(yīng)用于 AttributeDemo.DemoClass 的RecordAttribute屬性:

   AttributeDemo.RecordAttribute

      類型:更新

      作者:Matthew

      日期:2008-1-20

      備注:修改 ToString()方法

   AttributeDemo.RecordAttribute

      類型:更新

      作者:Jimmy

      日期:2008-1-18

   AttributeDemo.RecordAttribute

      類型:創(chuàng)建

      作者:張子陽(yáng)

      日期:2008-1-15

好了,到了這一步,我想將這些數(shù)據(jù)錄入數(shù)據(jù)庫(kù)中將不再是個(gè)問(wèn)題,我們關(guān)于C#反射查看自定義特性的章節(jié)也就到此為止了。

【編輯推薦】

  1. C#基礎(chǔ)知識(shí)一覽
  2. 學(xué)習(xí)C#自定義用戶控件
  3. C#自定義組件和用戶組件屬性的設(shè)置
  4. C#編程中的組件-事件-委托
  5. Visual C#自定義組件的設(shè)計(jì):Pop3Com組件
責(zé)任編輯:book05 來(lái)源: cnblogs
相關(guān)推薦

2009-08-04 08:58:01

C#自定義特性

2021-03-29 00:02:10

C#Attribute元素

2016-10-13 13:33:41

反射特性c#

2009-08-04 08:48:44

C#內(nèi)置特性

2009-08-03 13:34:06

自定義C#控件

2009-08-28 17:45:19

C#自定義數(shù)據(jù)

2009-08-03 13:39:46

C#自定義用戶控件

2009-09-03 15:46:57

C#自定義事件

2009-08-19 16:50:32

Visual C#C#語(yǔ)言特性

2009-08-26 17:10:09

C# 3.5新特性

2009-08-26 16:01:37

C#特性

2020-04-30 21:40:14

C#特性編程語(yǔ)言

2009-08-04 12:40:34

c#自定義事件

2009-08-12 14:53:50

C#類型轉(zhuǎn)換函數(shù)

2024-09-11 14:46:48

C#旋轉(zhuǎn)按鈕

2021-06-17 06:52:37

C#自定義異常

2009-08-04 12:56:51

C#自定義事件

2009-08-05 17:03:37

C#自定義控件

2009-08-04 09:56:46

C#事件處理自定義事件

2009-09-17 16:34:24

C#組件類
點(diǎn)贊
收藏

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