热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

13.定义自定义属性参数的访问属性翻译概述:一个比较无聊的规则,实在看不出在什么情况下,一个开发者会做出违反这条规则的设计。没有别的内容&

13.     定义自定义属性参数的访问属性

翻译概述:

一个比较无聊的规则,实在看不出在什么情况下,一个开发者会做出违反这条规则的设计。没有别的内容,只是说应该为自定义特性的构造函数中的参数提供一个相关的属性去读取它们的值。

一个让我比较费解的规则,即没有看出其中所传达的设计思想,也没发现任何优秀的设计技巧。

原文引用:

Define accessors for attribute arguments

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    
&#39; Missing the property that corresponds to 
InBlock.gif
    &#39; the someStringData parameter.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub New()Sub New(someStringData As String)
InBlock.gif        data 
&#61; someStringData
ExpandedSubBlockEnd.gif    
End Sub
 &#39;New
ExpandedSubBlockEnd.gif
End Class
 &#39;BadCustomAttribute
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif
&#39; 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 
&#61; someStringData
ExpandedSubBlockEnd.gif    
End Sub
 &#39;New
InBlock.gif

InBlock.gif    
&#39;The constructor parameter and property
InBlock.gif
    &#39;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 
&#61; 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 
&#61; 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

引起的原因&#xff1a;

没有为一个自定义特性&#xff08;Attribute&#xff09;的构造函数中的所有参数定义相应的属性来访问这些参数。

描述&#xff1a;

自定义特性可以定义一组强制参数&#xff0c;当将特性应用到目标上时&#xff0c;必须指定这些参数。因为他们被定义为特性的构造函数的位置参数&#xff08;非命名参数&#xff09;&#xff0c;通常称它们为位置参数。对于每一个强制参数&#xff0c;自定义特性应该同时提供一个相关的制度属性&#xff0c;这样&#xff0c;才能在需要的时候获得这些参数的值。这条规则检查你是否为每一个参数定义了相关属性。

自定义特性也可以定义可选参数&#xff0c;称之为命名参数。在自定义特性的构造函数中可以使用名字指定它们的值。应该为它们定义可读可写属性。

对于强制的和可选的参数&#xff0c;他们相关的属性应该和构造函数参数有类似的名字&#xff0c;仅仅使用大小写区分它们。&#xff08;属性使用Pascal命名规则&#xff0c;参数使用骆峰命名规则&#xff09;

修复&#xff1a;

为构造函数中的每一个参数提供一个只读属性。

例外&#xff1a;

如果你不打算获得强制参数的值&#xff0c;可以忽略这条规则。

例程&#xff1a;

原文中给出了两个自定义特性类&#xff08;分别给出了使用VB.NETC#的实现&#xff09;&#xff0c;它们都定义了一个强制参数。其中第一个自定义特性类违反了这条规则&#xff0c;没有为强制参数实现相关的属性。第二个自定义特性类修复了这个问题。


转载于:https://www.cnblogs.com/Cajon/archive/2005/06/28/182305.html


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在Mac上使用Pillow库加载不同于默认字体和大小的字体,并提供了一个简单的示例代码。通过该示例,读者可以了解如何在Python中使用Pillow库来写入不同字体的文本。同时,本文也解决了在Mac上使用Pillow库加载字体时可能遇到的问题。读者可以根据本文提供的示例代码,轻松实现在Mac上使用Pillow库加载不同字体的功能。 ... [详细]
author-avatar
年庚瑶
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有