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

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

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

  1. [FxCop.设计规则]16. 不要在封闭类中声明虚成员

    16.     不要在封闭类中声明虚成员 翻译概述: 一条比较无聊的规则,并且VB.NET和C#编译器都已经内嵌的禁止代码违反这条规则. 引起的原因: 一个公共的封闭类型中包含虚成员.这条规则不检查d ...

  2. 13 PP配置-生产主数据-BOM相关-定义修正参数

    业务背景:定义修正参数 事务码:OS27 SPRO路径:SPRO->生产->基本数据->物料清单->物料单控制数据->定义缺省值 第1步,SPRO进入 第2步,输入参数并 ...

  3. 代码走查工具篇FxCop的规则总结与翻译_Part2

    续接上篇:代码走查工具篇FxCop的规则总结与翻译_Part1 Globalizationrules(区域性规则) 1. AvoidDuplicateAccelerators  CA1301 避免快捷 ...

  4. AD20设计规则小结(Design Rules)

    一.Electrical(电器规则) 1.Clearence (线间距.铺铜间距设置规则) 常规情况下,铺铜间距可设置为线间距的2-3倍:且铺铜间距和线间距应该分开制定规则. 2.Short-Circ ...

  5. FPGA-Xilinx 7系列FPGA DDR3硬件设计规则

    Xilinx 7系列FPGA DDR3硬件设计规则 引言:本文我们介绍Xilinx 7系列FPGA DDR3硬件设计规则及约束,包括Bank选择.管脚位置约束.管脚分配.端接.I/O标准和走线长度. ...

  6. 设计规则之里氏替换原则

    tip: 作为程序员一定学习编程之道,一定要对代码的编写有追求,不能实现就完事了.我们应该让自己写的代码更加优雅,即使这会费时费力. 相关规则: 推荐:体系化学习Java(Java面试专题) 1.6大 ...

  7. ad中pcb双面板怎么设置_PCB的设计规则和加工要求参考

    多数工程师在研发期间做快板,一般为双面板,要保证自己设计出来的板子能够顺利加工且加工出来的效果符合自己的预期,需要在PCB布局布线的时候设定好正确的Design Rule(设计规则)并在投板的时候按照 ...

  8. python中函数包括参数函数吗_Python中的函数---函数的定义和参数

    本文是廖雪峰教程的笔记. 函数的定义 定义函数时,需要确定函数名和参数个数 def fun(x1,x2,x3):: 如果有必要,可以先对参数的数据类型做检查: 函数体内部可以用return随时返回函数 ...

  9. java定义一个空数组_一个 Java 方法,最多能定义多少参数?

    点击上方"JAVA",星标公众号重磅干货,第一时间送达 文链接:http://justinblank.com/experiments/howmanytypeparametersca ...

最新文章

  1. 【spring】使用构造方法依赖注入
  2. 除了Open Day,Nibiru与DigiArtist来CJ 搞事情了
  3. 受迫阻尼 matlab 仿真,MATLAB系统仿真报告——有阻尼受迫振动系统
  4. 深度学习之PyTorch物体检测实战——新书赠送活动
  5. java中notify是什么意思_java中wait,notify,notifyAll是什么?
  6. java 之 插入排序
  7. 《21天学通C语言》总结(2)
  8. 软件项目管理 第七章 进度计划
  9. DataStore详解
  10. VMware16安装MacOS Big Sur系统
  11. python访问字符串中的部分字符的操作_Python字符串基础操作
  12. 【黑马程序员pink老师前端】HTML
  13. 五、从命令行管理文件
  14. 30_Python基础_异常
  15. 万洲金业:黄金ETF如何影响黄金价格?
  16. 如何将word文档内容在网页显示方法
  17. 手写Android热修复
  18. TCP/UDP网络的通信
  19. e代驾——打造代驾服务标准化平台
  20. datagridview取消默认选中_DataGridView点击空白处和失去焦点后取消选择和关闭默认选择第一行C#Winform...

热门文章

  1. PHP + NGINX 控制视频文件播放,并防止文件下载
  2. 分布式锁与实现(一)——基于Redis实现
  3. KVM虚拟机共享存储动态迁移与冷迁移
  4. JAXB vs XStream
  5. ATS读小文件(内存命中)
  6. PHP ThinkPHP学习第一步(搭建及认识ThinkPHP入口文件)
  7. 如何在android模拟器中安装apk软件
  8. linux syslog 笔记
  9. GdiPlus[38]: IGPGraphicsPath (五) 路径标记
  10. Smoothing滤波处理halcon算子,持续更新