日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[FxCop.设计规则]13. 定义自定义属性参数的访问属性

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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]

Imports?System

Namespace?DesignLibraryNamespace?DesignLibrary

'?Violates?rule:?DefineAccessorsForAttributeArguments.
<AttributeUsage(AttributeTargets.All)>??_
NotInheritable?Public?Class?BadCustomAttributeClass?BadCustomAttribute
????
Inherits?Attribute
????
Private?data?As?String
????
????
'?Missing?the?property?that?corresponds?to?
????'?the?someStringData?parameter.
????Public?Sub?New()Sub?New(someStringData?As?String)
????????data?
=?someStringData
????
End?Sub
?'New
End?Class
?'BadCustomAttribute



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

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

End?Class
?

End?Namespace


[C#]

using?System;

namespace?DesignLibrary
{
//?Violates?rule:?DefineAccessorsForAttributeArguments.

???[AttributeUsage(AttributeTargets.All)]
???
public?sealed?class?BadCustomAttribute?:Attribute?
???
{
??????
string?data;

??????
//?Missing?the?property?that?corresponds?to?
??????
//?the?someStringData?parameter.

??????
public?BadCustomAttribute(string?someStringData)
??????
{
?????????data?
=?someStringData;
??????}

???}


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

???[AttributeUsage(AttributeTargets.All)]
???
public?sealed?class?GoodCustomAttribute?:Attribute?
???
{
??????
string?data;

??????
public?GoodCustomAttribute(string?someStringData)
??????
{
?????????data?
=?someStringData;
??????}

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

??????
public?string?SomeStringData
??????
{
?????????
get?
?????????
{
????????????
return?data;
?????????}

??????}

???}

}


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

總結

以上是生活随笔為你收集整理的[FxCop.设计规则]13. 定义自定义属性参数的访问属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。