مشاركة عبر


ca1018: السمات علامة مع AttributeUsageAttribute

TypeName

MarkAttributesWithAttributeUsage

CheckId

ca1018

Category

Microsoft.تصميم

تعطيل تغيير

فصل

السبب

System.AttributeUsageAttributeسمة هو غير موجودة تشغيل السمة المخصصة.

وصف القاعدة

عند تعريف سمة مخصصة، وضع علامة عليه باستخدام AttributeUsageAttributeللإشارة إلى مكان في تعليمات برمجية المصدر سمة مخصصة يمكن تطبيقها. معنى السمة و الغرض من استخدام ستحدد بها مواقع? صالحة في تعليمات برمجية. تشغيل سبيل المثال، إذا كنت تقوم بتعريف سمة تعريف الشخص المسؤول عن صيانة وتحسين كل نوع في المكتبة، ومسؤولية هو دوماً تعيين تشغيل المستوى النوع، compilers يجب السماح بالسمة تشغيل فئات والتعدادات والواجهات، ولكن يجب عدم السماح له تشغيل الأحداث أو الخصائص والأساليب. نهج تنظيمية و هل إملاء الإجراءات بما إذا كان يجب السماح للسمة تشغيل تجميعات.

System.AttributeTargetsتعريف التعداد أهداف يمكنك تحديد سمة مخصصة. إذا حذفت AttributeUsageAttribute، تكون صالحة لكافة أهداف السمة المخصصة الخاصة بك كـ معرفة بواسطةAll

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

إلى إصلاح انتهاكا لهذه قاعدة، قم بتحديد أهداف للسمة استخدام AttributeUsageAttribute. راجع المثال التالي.

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

يجب إصلاح انتهاكا لهذه قاعدة بدلاً من الرسالة باستثناء. حتى إذا كانت السمة ترث AttributeUsageAttribute، يجب أن تكون السمة موجودة بتبسيط تعليمات برمجية الصيانة.

مثال

يلي مثال يعرف سمتان. BadCodeMaintainerAttributeيحذف بشكل غير صحيحAttributeUsageAttributeالعبارة، بينماGoodCodeMaintainerAttributeبشكل صحيح بتطبيق سمة الموضحة أعلاه. لاحظ أن الخاصية DeveloperNameهو المطلوبة من قبل قاعدة التصميم ca1019: قم بتعريف accessors للوسائط السمةو هو المضمنة لاكتمال.

Imports System

Namespace DesignLibrary

' Violates rule: MarkAttributesWithAttributeUsage.
NotInheritable Public Class BadCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
End Class 

' Satisfies rule: Attributes specify AttributeUsage.
' The attribute is valid for type-level targets.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Enum Or _ 
   AttributeTargets.Interface Or AttributeTargets.Delegate)> _
NotInheritable Public Class GoodCodeMaintainerAttribute
    Inherits Attribute
    Private developer As String

    Public Sub New(developerName As String)
        developer = developerName
    End Sub 'New

    Public ReadOnly Property DeveloperName() As String
        Get
            Return developer
        End Get
    End Property
End Class 

End Namespace
using System;

namespace DesignLibrary
{
// Violates rule: MarkAttributesWithAttributeUsage.

   public sealed class BadCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public BadCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
// Satisfies rule: Attributes specify AttributeUsage.

   // The attribute is valid for type-level targets.
   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
   public sealed class GoodCodeMaintainerAttribute :Attribute 
   {
      string developer;

      public GoodCodeMaintainerAttribute(string developerName)
      {
         developer = developerName;
      }
      public string DeveloperName
      {
         get 
         {
            return developer;
         }
      }
   }
}

القواعد ذات الصلة

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

CA1813: Avoid unsealed attributes

راجع أيضًا:

المرجع

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

System.AttributeTargets