Share via


CA1019:定义特性参数的访问器

类型名

DefineAccessorsForAttributeArguments

CheckId

CA1019

类别

Microsoft.Design

是否重大更改

非重大更改

原因

在其构造函数中,特性定义了没有相应属性的参数。

规则说明

特性可以定义强制实参,在对目标应用该特性时必须指定这些实参。 这些实参也称为位置实参,因为它们将作为位置形参提供给特性构造函数。 对于每一个强制变量,特性还必须提供一个相应的只读属性,以便可以在执行时检索该变量的值。 该规则将检查您是否为每一个构造函数形参定义了相应的属性。

特性还可以定义可选实参,可选实参也称为命名实参。 这些变量按名称提供给特性构造函数,并且必须具有相应的读/写属性。

对于强制变量和可选变量,相应的属性和构造函数参数应使用相同的名称,但大小写形式不同。 属性使用 Pascal 大小写形式,参数使用 Camel 大小写形式。

如何解决冲突

要修复与该规则的冲突,请为每一个不具有只读属性的构造函数参数添加只读属性。

何时禁止显示警告

如果不希望强制参数的值可供检索,则可以禁止显示与该规则有关的警告。

自定义特性示例

说明

下面的示例演示两个定义强制(位置)参数的特性。 第一个特性实现的定义是错误的。 第二个实现是正确的。

代码

Imports System

Namespace DesignLibrary

' Violates rule: DefineAccessorsForAttributeArguments.
<AttributeUsage(AttributeTargets.All)>  _
NotInheritable Public Class BadCustomAttribute
    Inherits Attribute
    Private data As String

    ' Missing the property that corresponds to 
    ' the someStringData parameter.
    Public 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 GoodCustomAttribute
    Inherits Attribute
    Private data As String

    Public 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() As String
        Get
            Return data
        End Get
    End Property
End Class 

End Namespace
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;
         }
      }
   }
}

位置和命名参数

说明

位置和命名参数可以让库的使用者清楚哪些参数对于特性是强制的,哪些参数是可选的。

下面的示例演示具有位置和命名参数的特性的实现。

代码

using System; 

namespace DesignLibrary
{    
    [AttributeUsage(AttributeTargets.All)]        
    public sealed class GoodCustomAttribute : Attribute    
    {        
        string mandatory;        
        string optional;         

        public GoodCustomAttribute(string mandatoryData)        
        {            
            mandatory = mandatoryData;        
        }         

        public string MandatoryData        
        {            
            get { return mandatory; }        
        }         

        public string OptionalData        
        {            
            get { return optional; }            
            set { optional = value; }        
        }    
    }
}

注释

下面的示例演示如何将自定义特性应用于两个属性。

代码

[GoodCustomAttribute("ThisIsSomeMandatoryData", OptionalData = "ThisIsSomeOptionalData")]
public string MyProperty
{
    get { return myProperty; }
    set { myProperty = value; }
}

[GoodCustomAttribute("ThisIsSomeMoreMandatoryData")]
public string MyOtherProperty
{
    get { return myOtherProperty; }
    set { myOtherProperty = value; }
}

相关规则

CA1813:避免使用未密封的特性

请参见

参考

特性使用指南