[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.gif Imports  System
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Namespace DesignLibrary Namespace 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.gif using  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#的实现),它们都定义了一个强制参数。其中第一个自定义特性类违反了这条规则,没有为强制参数实现相关的属性。第二个自定义特性类修复了这个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值