[FxCop.設計規則]13. 定義自定義屬性參數的訪問屬性

13.???? 定義自定義屬性參數的訪問屬性

翻譯概述:

一個比較無聊的規則,實在看不出在什么情況下,一個開發者會做出違反這條規則的設計。沒有別的內容,只是說應該為自定義特性的構造函數中的參數提供一個相關的屬性去讀取它們的值。

一個讓我比較費解的規則,即沒有看出其中所傳達的設計思想,也沒發現任何優秀的設計技巧。

原文引用:

Define accessors for attribute arguments<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

TypeName:

DefineAccessorsForAttributeArguments

CheckId:

CA1019

Category:

Microsoft.Design

Message Level:

Error

Certainty:

95%

Breaking Change:

NonBreaking


Cause: In its constructor, an attribute defines arguments that do not have corresponding properties.

Rule Description

Attributes can define mandatory arguments that must be specified when applying the attribute to a target. These are sometimes called positional arguments because they are supplied to attribute constructors as positional parameters. For every mandatory argument, the attribute should also provide a corresponding read-only property so that the value of the argument can be retrieved at execution time. This rule checks to see that for each constructor parameter, you have defined the corresponding property.

Attributes can also define optional arguments, called named arguments. These arguments are supplied to attribute constructors by name and should have a corresponding read/write property.

For mandatory and optional arguments, the corresponding properties and constructor parameters should use the same name but different casing. (Properties use Pascal casing, and parameters use camel casing.)

How to Fix Violations

To fix a violation of this rule, add a read-only property for each constructor parameter that does not have one.

When to Exclude Messages

Exclude a message from this rule if you do not want the value of the mandatory argument to be retrievable.

Example Code

The following example shows two attributes that define a mandatory (positional) parameter. The first implementation of the attribute is incorrectly defined. The second implementation is correct.

[Visual Basic]

None.gifImports?System
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Namespace?DesignLibraryNamespace?DesignLibrary
InBlock.gif
InBlock.gif
'?Violates?rule:?DefineAccessorsForAttributeArguments.
InBlock.gif
<AttributeUsage(AttributeTargets.All)>??_
ExpandedSubBlockStart.gifContractedSubBlock.gif
NotInheritable?Public?Class?BadCustomAttributeClass?BadCustomAttribute
InBlock.gif????
Inherits?Attribute
InBlock.gif????
Private?data?As?String
InBlock.gif????
InBlock.gif????
'?Missing?the?property?that?corresponds?to?
InBlock.gif
????'?the?someStringData?parameter.
ExpandedSubBlockStart.gifContractedSubBlock.gif
????Public?Sub?New()Sub?New(someStringData?As?String)
InBlock.gif????????data?
=?someStringData
ExpandedSubBlockEnd.gif????
End?Sub
?'New
ExpandedSubBlockEnd.gif
End?Class
?'BadCustomAttribute
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif
'?Satisfies?rule:?Attributes?should?have?accessors?for?all?arguments.
InBlock.gif
<AttributeUsage(AttributeTargets.All)>??_
ExpandedSubBlockStart.gifContractedSubBlock.gif
NotInheritable?Public?Class?GoodCustomAttributeClass?GoodCustomAttribute
InBlock.gif????
Inherits?Attribute
InBlock.gif????
Private?data?As?String
InBlock.gif????
ExpandedSubBlockStart.gifContractedSubBlock.gif????
Public?Sub?New()Sub?New(someStringData?As?String)
InBlock.gif????????data?
=?someStringData
ExpandedSubBlockEnd.gif????
End?Sub
?'New
InBlock.gif

InBlock.gif????
'The?constructor?parameter?and?property
InBlock.gif
????'name?are?the?same?except?for?case.
InBlock.gif
????
ExpandedSubBlockStart.gifContractedSubBlock.gif????
Public?ReadOnly?Property?SomeStringData()Property?SomeStringData()?As?String
InBlock.gif????????
Get
InBlock.gif????????????
Return?data
InBlock.gif????????
End?Get
ExpandedSubBlockEnd.gif????
End?Property

ExpandedSubBlockEnd.gif
End?Class
?
InBlock.gif
ExpandedBlockEnd.gif
End?Namespace

None.gif


[C#]

None.gifusing?System;
None.gif
None.gif
namespace?DesignLibrary
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
//?Violates?rule:?DefineAccessorsForAttributeArguments.
InBlock.gif

InBlock.gif???[AttributeUsage(AttributeTargets.All)]
InBlock.gif???
public?sealed?class?BadCustomAttribute?:Attribute?
ExpandedSubBlockStart.gifContractedSubBlock.gif???
dot.gif{
InBlock.gif??????
string?data;
InBlock.gif
InBlock.gif??????
//?Missing?the?property?that?corresponds?to?
InBlock.gif??????
//?the?someStringData?parameter.
InBlock.gif

InBlock.gif??????
public?BadCustomAttribute(string?someStringData)
ExpandedSubBlockStart.gifContractedSubBlock.gif??????
dot.gif{
InBlock.gif?????????data?
=?someStringData;
ExpandedSubBlockEnd.gif??????}

ExpandedSubBlockEnd.gif???}

InBlock.gif
InBlock.gif
//?Satisfies?rule:?Attributes?should?have?accessors?for?all?arguments.
InBlock.gif

InBlock.gif???[AttributeUsage(AttributeTargets.All)]
InBlock.gif???
public?sealed?class?GoodCustomAttribute?:Attribute?
ExpandedSubBlockStart.gifContractedSubBlock.gif???
dot.gif{
InBlock.gif??????
string?data;
InBlock.gif
InBlock.gif??????
public?GoodCustomAttribute(string?someStringData)
ExpandedSubBlockStart.gifContractedSubBlock.gif??????
dot.gif{
InBlock.gif?????????data?
=?someStringData;
ExpandedSubBlockEnd.gif??????}

InBlock.gif??????
//The?constructor?parameter?and?property
InBlock.gif??????
//name?are?the?same?except?for?case.
InBlock.gif

InBlock.gif??????
public?string?SomeStringData
ExpandedSubBlockStart.gifContractedSubBlock.gif??????
dot.gif{
InBlock.gif?????????
get?
ExpandedSubBlockStart.gifContractedSubBlock.gif?????????
dot.gif{
InBlock.gif????????????
return?data;
ExpandedSubBlockEnd.gif?????????}

ExpandedSubBlockEnd.gif??????}

ExpandedSubBlockEnd.gif???}

ExpandedBlockEnd.gif}


Related Rules

Avoid unsealed attributes

See Also

Attribute Usage Guidelines

引起的原因:

沒有為一個自定義特性(Attribute)的構造函數中的所有參數定義相應的屬性來訪問這些參數。

描述:

自定義特性可以定義一組強制參數,當將特性應用到目標上時,必須指定這些參數。因為他們被定義為特性的構造函數的位置參數(非命名參數),通常稱它們為位置參數。對于每一個強制參數,自定義特性應該同時提供一個相關的制度屬性,這樣,才能在需要的時候獲得這些參數的值。這條規則檢查你是否為每一個參數定義了相關屬性。

自定義特性也可以定義可選參數,稱之為命名參數。在自定義特性的構造函數中可以使用名字指定它們的值。應該為它們定義可讀可寫屬性。

對于強制的和可選的參數,他們相關的屬性應該和構造函數參數有類似的名字,僅僅使用大小寫區分它們。(屬性使用Pascal命名規則,參數使用駱峰命名規則)

修復:

為構造函數中的每一個參數提供一個只讀屬性。

例外:

如果你不打算獲得強制參數的值,可以忽略這條規則。

例程:

原文中給出了兩個自定義特性類(分別給出了使用VB.NETC#的實現),它們都定義了一個強制參數。其中第一個自定義特性類違反了這條規則,沒有為強制參數實現相關的屬性。第二個自定義特性類修復了這個問題。

轉載于:https://www.cnblogs.com/Cajon/archive/2005/06/28/182305.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/277314.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/277314.shtml
英文地址,請注明出處:http://en.pswp.cn/news/277314.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C語言程序設計實驗最短路徑,7最短路徑C語言程序設計.pdf

最短路徑旅行家的困擾第4章 圖結構 解放軍理工大學旅行家的困擾新疆特克斯縣“八卦城”第4章 圖結構 解放軍理工大學旅行家的困擾特克斯縣怎么樣幫助困擾的旅行家找到去各個地點的最短路線呢&#xff1f;旅行家居住的旅館旅行家想去的地點第4章 圖結構 解放軍理工大學問題建模使…

centos7安裝Cloudera Manager

第一部分&#xff1a;準備工作一&#xff0c;修改hostname $vim /etc/sysconfig/network $source /etc/sysconfig/network例如&#xff1a; NETWORKINGyes HOSTNAMEspark01reboot重啟服務器 二&#xff0c;關閉selinux查看SELinux狀態1&#xff0c;/usr/sbin/sestatus -v #如果…

He Fei ,First ,Good Luck

Tonight, I will go to HeFei.something as follows:1) speciality 2) sincerely3) valueHope i can bring customer some ideas .But i will throw my 100% energy to face it.Good Luck.First HeFei轉載于:https://www.cnblogs.com/boriscao/archive/2005/08/31/227199.html…

在C#中使用代理的方式觸發事件 的簡單習作

程序簡單就 不再作說明了.在學習IssueVision的OBSERVER(觀察者)模式時由于對代理和事件不是很熟悉,遇到了一些問題,所以就有了這個簡單習作.TestEvent.cs using System; namespace ConsoleApplication2{ /// <summary> /// Class1 的摘要說明。 /// </summar…

c語言遞歸有何作用,c語言中,什么是函數的遞歸,能舉個例子么

(PS:因為很多IT術語的定義都來源于國外&#xff0c;我們看的中文大部分是別人看了國外的文獻然后以他的中文素養加以解釋的&#xff01;但是中華語言博大精深&#xff01;而英語就較為簡單了&#xff0c;記得上次看高德納的《surrealnumber》時候&#xff0c;文中有一句“thebe…

Nodejs 文件上傳

node js 接收表單數據原理 /*** node js 接收表單數據*/ const http require("http"); const qs require("querystring");http.createServer((request, response) > {// 表單提交的原理if (request.url "/post" && request.meth…

c語言程序設計 銀行整存整取,《C語言程序設計習題試題集》.doc

《C語言程序設計習題試題集》.doc1、已知銀行整存整取存款不同期限的月息利率分別為&#xff1a;0.315% 期限一年0.330% 期限二年0.345% 期限三年0.375% 期限五年0.420% 期限八年要求輸入存錢的本金和期限&#xff0c;求到期時能從銀行得到的利息與本金的合計。2、輸入年份year…

Grove——.NET中的ORM實現

Grove——.NET中的ORM實現 發布日期&#xff1a; 6/30/2005| 更新日期&#xff1a; 6/30/2005作者&#xff1a;林學鵬 ORM的全稱是Object Relational Mapping&#xff0c;即對象關系映射。它的實質就是將關系數據&#xff08;庫&#xff09;中的業務數據用對象的形式表示出來&a…

let和const

發布自Kindem的博客&#xff0c;歡迎大家轉載&#xff0c;但是要注意注明出處。另外&#xff0c;該文章收納在Kindem的個人的 IT 知識整理倉庫&#xff0c;歡迎 Star、Fork、投稿 let let是在ES6加入的新的變量聲明方法&#xff0c;let聲明變量的方法和var類似: let a hello; …

GIT 操作筆記

設置不用每次都輸入 賬號密碼 git config --global credential.helper store然后&#xff0c;下次再輸入一次 賬號密碼 就可以了。刪除github上的文件$ git pull origin master 將遠程倉庫里面的項目拉下來$ dir 查看有哪些文件夾$ git rm -r --cached target 刪除target文件…

[book]道法自然

前不久讀了王詠剛的〈凌波微步〉和〈凌波微步II〉&#xff0c;感覺不錯。今天把他老人家的《道法自然》也買了下來。在dearbook看到關于這本書的長篇大評&#xff0c;也一塊copy了下來&#xff1a;http://www.dearbook.com.cn/book/viewbook.aspx?pnoTS0023954認真的作者&…

c語言編手機藍牙軟件的代碼,51單片機C語言的簡易藍牙鎖代碼

#include#define uint unsigned int#define uchar unsigned charuchar r_buf; //藍牙接收到的字符存儲緩沖區sbit PWMAP2^0; //電機驅動模塊接口sbit IN1P2^2;sbit IN2P2^1;sbit STBYP2^3;sbit LED1P0^0; //LED燈接口sbit LED2P0^1;sbit LED3P0^2;sbit key1P1^0;…

列表嵌套字典,根據字典某一key排序

在返回列表嵌套字典時候&#xff0c;往往需要對數據進行一定的處理&#xff1a;按照字典中某一個key排序 In [87]: a [{"name": "牛郎", "age": 23},{"name":"許仙", "age": 20},{"name":"董永&q…

display: inline-block 布局

三個元素display: inline-block; 布局 &#xff0c;其中一個元素中存在其他元素也用了display: inline-block; 無法垂直居中&#xff0c;將這個元素設置為display: inline; 轉載于:https://www.cnblogs.com/Running00/p/11163751.html

GCC精彩之旅

在為Linux開發應用程序時&#xff0c;絕大多數情況下使用的都是C語言&#xff0c;因此幾乎每一位Linux程序員面臨的首要問題都是如何靈活運用C編譯器。目前 Linux下最常用的C語言編譯器是GCC&#xff08;GNU Compiler Collection&#xff09;&#xff0c;它是GNU項目中符合ANSI…

寫出C語言中5種數據類型的名稱及其關鍵字,求C語言中的32個關鍵字及其意思?...

關鍵字如下&#xff1a;一、數據類型關鍵字(12個)&#xff1a;(1) char &#xff1a;聲明字符型變量或函數(2) double &#xff1a;聲明雙精度變量或函數(3) enum &#xff1a;聲明枚舉類型(4) float&#xff1a;聲明浮點型變量或函數(5) int&#xff1a; 聲明整型變量或函數(6…

想要設計自己的微服務?看這篇文章就對了

歡迎大家前往騰訊云社區&#xff0c;獲取更多騰訊海量技術實踐干貨哦~ 本文由我就靜靜地看 發表于云社區專欄 本文通過使用Spring Boot&#xff0c;Spring Cloud和Docker構建的概念驗證應用程序的示例&#xff0c;為了解常見的微服務架構模式提供了一個起點。 該代碼在Github上…

函數的嵌套調用

在函數的內部調用其他的函數 def index(): func() print("index") def func(): index() # def index():# func()# print("index")## def func():# print("func")# index()#def my_max(x,y): #這個函數的作用就是比大小誰大打印出…

mysql 開發進階篇系列 41 mysql日志之慢查詢日志

一.概述 慢查詢日志記錄了所有的超過sql語句( 超時參數long_query_time單位 秒&#xff09;&#xff0c;獲得表鎖定的時間不算作執行時間。慢日志默認寫入到參數datadir(數據目錄)指定的路徑下。默認文件名是[hostname]_slow.log&#xff0c;默認超時是10秒&#xff0c;默認不開…

分數相同名次排名規則C語言,如何給數據排名(相同分數相同名次)-excel篇

使用Rank函數來做數據排名該函數是返回一個數值在一個數字列表中的排名。語法&#xff1a;RANK(number,ref,order)RANK(對象,范圍,參數)number(必填參數):是特定單位格中的數據&#xff0c;需要在整個數字列表中排名的單個對象。ref(必填參數):是指需要排名的整體數列。即范圍&…