自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊一聊C#創(chuàng)建自定義特性

開發(fā) 后端
特性(Attribute)是用于在運(yùn)行時(shí)傳遞程序中各種元素(比如類、方法、結(jié)構(gòu)、枚舉、組件等)的行為信息的聲明性標(biāo)簽。您可以通過使用特性向程序添加聲明性信息。

[[390099]]

本文轉(zhuǎn)載自微信公眾號(hào)「后端Q」,作者conan。轉(zhuǎn)載本文請(qǐng)聯(lián)系后端Q公眾號(hào)。

概述

特性(Attribute)是用于在運(yùn)行時(shí)傳遞程序中各種元素(比如類、方法、結(jié)構(gòu)、枚舉、組件等)的行為信息的聲明性標(biāo)簽。您可以通過使用特性向程序添加聲明性信息。一個(gè)聲明性標(biāo)簽是通過放置在它所應(yīng)用的元素前面的方括號(hào)([ ])來(lái)描述的。要設(shè)計(jì)你自己的自定義特性,無(wú)需掌握許多新的概念。 如果你熟悉面向?qū)ο蟮木幊蹋⑶抑廊绾卧O(shè)計(jì)類,那么你已經(jīng)具備大部分所需知識(shí)。 自定義特性本質(zhì)上是直接或間接派生自 System.Attribute的傳統(tǒng)類。 與傳統(tǒng)類一樣,自定義特性包含用于存儲(chǔ)和檢索數(shù)據(jù)的方法。

實(shí)現(xiàn)方式

1、聲明自定義特性,一個(gè)新的自定義特性應(yīng)派生自 System.Attribute 類。

  1. // 一個(gè)自定義特性 BugFix 被賦給類及其成員 
  2. [AttributeUsage(AttributeTargets.Class | 
  3. AttributeTargets.Constructor | 
  4. AttributeTargets.Field | 
  5. AttributeTargets.Method | 
  6. AttributeTargets.Property, 
  7. AllowMultiple = true)] 
  8.  
  9. public class DeBugInfo : System.Attribute 

2、構(gòu)建自定義特性,讓我們構(gòu)建一個(gè)名為 DeBugInfo 的自定義特性,該特性將存儲(chǔ)調(diào)試程序獲得的信息。

  1. // 一個(gè)自定義特性 BugFix 被賦給類及其成員 
  2. [AttributeUsage(AttributeTargets.Class | 
  3. AttributeTargets.Constructor | 
  4. AttributeTargets.Field | 
  5. AttributeTargets.Method | 
  6. AttributeTargets.Property, 
  7. AllowMultiple = true)] 
  8.  
  9. public class DeBugInfo : System.Attribute 
  10.   private int bugNo; 
  11.   private string developer; 
  12.   private string lastReview; 
  13.   public string message; 
  14.  
  15.   public DeBugInfo(int bg, string dev, string d) 
  16.       this.bugNo = bg; 
  17.       this.developer = dev; 
  18.       this.lastReview = d; 
  19.   } 
  20.  
  21.   public int BugNo 
  22.   { 
  23.       get 
  24.       { 
  25.           return bugNo; 
  26.       } 
  27.   } 
  28.   public string Developer 
  29.   { 
  30.       get 
  31.       { 
  32.           return developer; 
  33.       } 
  34.   } 
  35.   public string LastReview 
  36.   { 
  37.       get 
  38.       { 
  39.           return lastReview; 
  40.       } 
  41.   } 
  42.   public string Message 
  43.   { 
  44.       get 
  45.       { 
  46.           return message; 
  47.       } 
  48.       set 
  49.       { 
  50.           message = value; 
  51.       } 
  52.   } 

3、應(yīng)用自定義特性

  1. [DeBugInfo(45, "Zara Ali""12/8/2012", Message = "Return type mismatch")] 
  2. [DeBugInfo(49, "Nuha Ali""10/10/2012", Message = "Unused variable")] 
  3. class Rectangle 
  4.   // 成員變量 
  5.   protected double length; 
  6.   protected double width; 
  7.   public Rectangle(double l, double w) 
  8.   { 
  9.       length = l; 
  10.       width = w; 
  11.   } 
  12.   [DeBugInfo(55, "Zara Ali""19/10/2012"
  13.   Message = "Return type mismatch")] 
  14.   public double GetArea() 
  15.   { 
  16.       return length * width; 
  17.   } 
  18.   [DeBugInfo(56, "Zara Ali""19/10/2012")] 
  19.   public void Display() 
  20.   { 
  21.       Console.WriteLine("Length: {0}", length); 
  22.       Console.WriteLine("Width: {0}", width); 
  23.       Console.WriteLine("Area: {0}", GetArea()); 
  24.   } 

 

責(zé)任編輯:武曉燕 來(lái)源: 后端Q
相關(guān)推薦

2022-10-09 08:35:06

SQL自定義排序

2009-08-04 08:58:01

C#自定義特性

2009-08-04 09:09:51

C#反射

2024-08-26 14:46:57

2024-12-26 10:05:58

C#前臺(tái)線程

2020-10-30 07:11:31

C 語(yǔ)言編程

2024-06-28 12:47:29

C#弱引用底層

2022-08-30 07:39:57

C++namespace隔離

2020-10-23 07:00:00

C++函數(shù)

2021-06-17 06:52:37

C#自定義異常

2023-12-07 07:26:04

2022-11-02 08:51:01

2024-09-11 14:46:48

C#旋轉(zhuǎn)按鈕

2024-01-02 13:26:39

TLSC#線程

2025-01-10 08:15:22

C#異步底層

2021-05-12 18:02:23

方法創(chuàng)建線程

2023-09-22 17:36:37

2021-01-28 22:31:33

分組密碼算法

2020-05-22 08:16:07

PONGPONXG-PON

2020-03-31 10:08:15

零信任安全軟件
點(diǎn)贊
收藏

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