CA1019:必須定義屬性引數的存取子

型別名稱

DefineAccessorsForAttributeArguments

CheckId

CA1019

分類

Microsoft.Design

中斷變更

中斷

原因

在它的建構函式 (Constructor) 中,屬性 (Attribute) 所定義的引數沒有對應的屬性 (Property)。

規則描述

屬性可以定義必須在將屬性套用至目標時指定的強制引數。這些引數也稱為位置引數,因為它們會當做位置參數提供給屬性建構函式。對於每個強制引數而言,屬性 (Attribute) 還須提供對應的唯讀屬性 (Property),才可以在執行時期擷取引數值。此規則會檢查每個建構函式參數,以查看您是否已為它們定義對應的屬性。

屬性也可以定義選擇性引數,也稱為具名引數。這些引數會依照名稱提供給屬性 (Attribute) 建構函式,且必須有對應的讀取/寫入屬性 (Property)。

對於強制和選擇性引數而言,對應的屬性和建構函式參數應使用相同的名稱,但大小寫不同。屬性會採用 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; }        
        }    
    }
}

註解

下列範例顯示如何套用自訂屬性 (Attribute) 到兩個屬性 (Property):

程式碼

[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:避免使用非密封屬性

請參閱

其他資源

Attribute Usage Guidelines