提供 ObsoleteAttribute 訊息
更新:2007 年 11 月
型別名稱 |
ProvideObsoleteAttributeMessage |
CheckId |
CA1041 |
分類 |
Microsoft.Design |
中斷變更 |
非中斷 |
原因
型別或成員是以 System.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";
}
}
};
}