共用方式為


CA1041:提供 ObsoleteAttribute 訊息

型別名稱

ProvideObsoleteAttributeMessage

CheckId

CA1041

分類

Microsoft.Design

中斷變更

中斷

原因

型別或成員是以 ObsoleteAttribute 屬性 (Attribute) 標記,但該屬性 (Attribute) 並未指定它的 ObsoleteAttribute.Message 屬性 (Property)。

規則描述

ObsoleteAttribute 會用於標記要取代的程式庫型別和成員。程式庫消費者應避免使用標記為過時的任何型別或成員。這是因為它可能不受支援,而且最後會從較新版本的程式庫中移除。編譯使用 ObsoleteAttribute 屬性所標記的型別或成員時,會顯示該屬性 (Attribute) 的 Message 屬性 (Property)。以便提供使用者有關過時型別或成員的資訊。此資訊通常包括程式庫設計工具將會支援過時型別或成員的時間,以及所要使用的慣用取代項目。

如何修正違規

若要修正此規則的違規情形,請將 message 參數加入至 ObsoleteAttribute 建構函式。

隱藏警告的時機

因為 Message 屬性 (Property) 會提供過時型別或成員的重要資訊,所以請勿隱藏這項規則的警告。

範例

下列範例會顯示具有正確宣告之 ObsoleteAttribute 的過時成員。

Imports System

Namespace DesignLibrary

    Public Class ObsoleteAttributeOnMember

        <ObsoleteAttribute("This property is obsolete and will " & _
             "be removed in a future version. Use the FirstName " & _
             "and LastName properties instead.", False)> _
        ReadOnly Property Name As String 
            Get 
                Return "Name" 
            End Get 
        End Property 

        ReadOnly Property FirstName As String 
            Get 
                Return "FirstName" 
            End Get 
        End Property 

        ReadOnly Property LastName As String 
            Get 
                Return "LastName" 
            End Get 
        End Property 

    End Class 

End Namespace
using System;

namespace DesignLibrary
{
    public class ObsoleteAttributeOnMember
    {
        [ObsoleteAttribute("This property is obsolete and will " +
             "be removed in a future version. Use the FirstName " +
             "and LastName properties instead.", false)]
        public string Name
        {
            get
            {
                return "Name";
            }
        }

        public string FirstName
        {
            get
            {
                return "FirstName";
            }
        }

        public string LastName
        {
            get
            {
                return "LastName";
            }
        }

    }
}
using namespace System;

namespace DesignLibrary
{
    public ref class ObsoleteAttributeOnMember
    {
    public:
        [ObsoleteAttribute("This property is obsolete and will "  
            "be removed in a future version. Use the FirstName "  
            "and LastName properties instead.", false)]
        property String^ Name
        {
            String^ get()
            {
               return "Name";
            }
        }

        property String^ FirstName
        {
            String^ get()
            {
               return "FirstName";
            }
        }

        property String^ LastName
        {
            String^ get()
            {
               return "LastName";
            }
        }
    };
}

請參閱

參考

ObsoleteAttribute