مشاركة عبر


ca1019: قم بتعريف accessors للوسائط السمة

TypeName

DefineAccessorsForAttributeArguments

CheckId

ca1019

Category

Microsoft.تصميم

تعطيل تغيير

غير فاصلة

السبب

في به الدالة الإنشائية، سمة تعريف الوسيطات التي لا تتضمن الخصائص المطابق.

وصف القاعدة

يمكنك تعريف سمات الوسيطات mandaإلىry التي يجب أن يتم تحديد عند يطبق السمة إلى هدف. وهي تدعي أيضا الوسيطات الموضعية سبب فإنها تقوم بتزويد إلى السمة construcإلىrs كمعلمات الموضعية. لكل وسيطة إلزامية، سمة يجب أن توفر أيضا المقابلة خاصية القراءة فقط حيث يمكن استرداد القيمة الوسيطة في وقت التنفيذ. فحص هذه قاعدة التي لكل الدالة الإنشائية المعلمات، قمت بتعريف الخصائص المطابق.

يمكنك أيضا تعريف الوسيطات الاختيارية، والتي تعرف أيضا على السمات كـ الوسيطات المسماة. يتم توفير هذه الوسيطات إلى السمة construcإلىrs باسمه ويجب أن يكون لخاصية قراءه/كتابه المطابق.

الوسيطات الإلزامية والاختيارية، التابع لها خصائص ومعلمات الدالة الإنشائية يجب استخدام نفس الاسم ولكنها مختلفة لحالة الأحرف. استخدام خصائص تسمية Pascal casing، و استخدام معلمات camel لحالة الأحرف.

كيف إلى الإصلاح انتهاكات

إلى إصلاح انتهاكا لهذه قاعدة، قم بإضافة خاصية للقراءة فقط لكل معلمة construcإلىr لم يكن أحد.

عند إلى منع التحذيرات

منع ظهور تحذير من هذه قاعدة إذا لم تكن تريد القيمة وسيطة mandaإلىry إلى أن retrievable.

مخصص السمات مثال

الوصف

يلي مثال يظهر سمتان تعريف معلمة إلزامية (الموضعية). تطبيق السمة الأولى هو المعرفة بشكل غير صحيح. التطبيق الثاني هو الصحيح.

الرمز

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

الموضعية و الوسيطات المسماة

الوصف

جعل الوسائط الموضعية والمسمى إلى مسح إلى مستخدمين للمكتبة الخاصة بك الوسائط التي يتم mandaإلىry للسمة والوسيطات التي تكون اختيارية.

يظهر المثال التالي تطبيق سمة يحتوي على كل من الموضعية و الوسيطات المسماة.

الرمز

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

التعليقات

يوضح المثال التالي كيفية إلى يطبق السمة cusإلىm إلى خاصيتين.

الرمز

[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: Avoid unsealed attributes

راجع أيضًا:

المرجع

إرشادات استخدام السمة