Share via


CA1041:提供 ObsoleteAttribute 消息

类型名

ProvideObsoleteAttributeMessage

CheckId

CA1041

类别

Microsoft.Design

是否重大更改

非重大更改

原因

类型或成员是使用未指定其 ObsoleteAttribute.Message 属性的 System.ObsoleteAttribute 特性进行标记的。

规则说明

ObsoleteAttribute 用于标记被否决的库类型和成员。 库使用者应避免使用任何标记为已过时的类型或成员。 这是因为它可能不被支持,并将最终将从库的更高版本中被删除。 当编译用 ObsoleteAttribute 标记的类型或成员时,将显示特性的 Message 属性。 这将为用户提供有关已过时的类型或成员的信息。 该信息通常包括库设计器还将支持已过时类型或成员的时间长度,以及首选的替代类型或成员。

如何解决冲突

要修复与该规则的冲突,请将 message 参数添加到 ObsoleteAttribute 构造函数。

何时禁止显示警告

由于 Message 属性提供有关已过时类型或成员的关键信息,因此不要禁止显示此规则发出的警告。

示例

下面的示例演示一个具有正确声明的 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";
            }
        }
    };
}

请参见

参考

System.ObsoleteAttribute