ObsoleteAttribute 類別

定義

標記不再使用的程式項目。Marks the program elements that are no longer in use. 此類別無法獲得繼承。This class cannot be inherited.

public ref class ObsoleteAttribute sealed : Attribute
public sealed class ObsoleteAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
public sealed class ObsoleteAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
[System.Serializable]
public sealed class ObsoleteAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ObsoleteAttribute : Attribute
type ObsoleteAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
type ObsoleteAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Serializable>]
type ObsoleteAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ObsoleteAttribute = class
    inherit Attribute
Public NotInheritable Class ObsoleteAttribute
Inherits Attribute
繼承
ObsoleteAttribute
屬性

範例

下列範例會定義包含屬性的類別,以及以屬性標記的方法 ObsoleteAttributeThe following example defines a class that contains a property and a method that are marked with the ObsoleteAttribute attribute. 存取程式 OldProperty 代碼中的屬性值會產生編譯器警告,但是呼叫方法會 CallOldMethod 產生編譯器錯誤。Accessing the value of the OldProperty property in code generates a compiler warning, but calling the CallOldMethod method generates a compiler error. 此範例也會顯示當您嘗試編譯原始程式碼時所產生的輸出。The example also shows the output that results when you attempt to compile the source code.

using System;
using System.Reflection;

public class Example
{
   // Mark OldProperty As Obsolete.
   [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)]
   public static string OldProperty
   { get { return "The old property value."; } }

   public static string NewProperty
   { get { return "The new property value."; } }

   // Mark CallOldMethod As Obsolete.
   [ObsoleteAttribute("This method is obsolete. Call CallNewMethod instead.", true)]
   public static string CallOldMethod()
   {
      return "You have called CallOldMethod.";
   }

   public static string CallNewMethod()
   {
      return "You have called CallNewMethod.";
   }

   public static void Main()
   {
      Console.WriteLine(OldProperty);
      Console.WriteLine();
      Console.WriteLine(CallOldMethod());
   }
}
// The attempt to compile this example produces output like the following output:
//    Example.cs(31,25): error CS0619: 'Example.CallOldMethod()' is obsolete:
//            'This method is obsolete. Call CallNewMethod instead.'
//    Example.cs(29,25): warning CS0618: 'Example.OldProperty' is obsolete:
//            'This property is obsolete. Use NewProperty instead.'
Imports System.Reflection

Public Module Example
   ' Mark OldProperty As Obsolete.
   <ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", False)> 
   Public ReadOnly Property OldProperty As String
      Get
         Return "The old property value."
      End Get
   End Property
   
   Public ReadOnly Property NewProperty As String
      Get
         Return "The new property value."
      End Get
   End Property
   
   ' Mark OldMethod As Obsolete.
   <ObsoleteAttribute("This method is obsolete. Call CallNewMethod instead.", True)> 
   Public Function CallOldMethod() As String
      Return "You have called CallOldMethod."
   End Function
      
   Public Function CallNewMethod() As String   
      Return "You have called NewMethod."
   End Function   
   
   Public Sub Main()
      Console.WriteLine(OldProperty)
      Console.WriteLine()
      Console.WriteLine(CallOldMethod())
   End Sub  
End Module
'  The attempt to compile this example produces output like the following:
'    Example.vb(30) : warning BC40000: 'Public ReadOnly Property OldProperty As String' is obsolete: 
'                     'This property is obsolete. Use NewProperty instead.'.
'    
'          Console.WriteLine(OldProperty)
'                            ~~~~~~~~~~~
'    Example.vb(32) : error BC30668: 'Public Function CallOldMethod() As String' is obsolete: 
'                     'This method is obsolete. Call CallNewMethod instead.'.
'    
'          Console.WriteLine(CallOldMethod())
'                            ~~~~~~~~~~~~~

備註

ObsoleteAttribute 適用于所有的程式元素,但元件、模組、參數和傳回值除外。ObsoleteAttribute is applicable to all program elements except assemblies, modules, parameters, and return values. 將專案標記為過時,會通知使用者未來的產品版本中可能會移除該元素。Marking an element as obsolete informs users that the element may be removed in a future version of the product.

Message當程式碼中使用屬性目標時,編譯器會發出指派給屬性的字串。The string assigned to the Message property is emitted by the compiler when the attribute target is used in code. 在理想情況下,此字串應該提供一些因應措施或以程式設計方式替代。Ideally, the string should provide some workaround or programmatic alternative.

您可以使用 IsError 屬性來向編譯器指出使用屬性是否 ObsoleteAttribute 應該使其發出錯誤 (IsError true) 或) (的警告 IsError falseUse the IsError property to indicate to the compiler whether using the ObsoleteAttribute attribute should cause it to emit an error (IsError is true) or a warning (IsError is false).

如需使用屬性的詳細資訊,請參閱 屬性For more information about using attributes, see Attributes.

Windows 8. x 商店應用程式Windows 8.x Store apps

當您建立 Windows 元資料連結庫 ( 的 winmd 檔) 時, ObsoleteAttribute 如果只有在原始程式碼中,則會匯出為 ObsoleteAttribute 屬性和>windows.foundation.deprecatedattribute屬性。 ObsoleteAttributeWhen you create a Windows Metadata library (.winmd file), the ObsoleteAttribute is exported as both the ObsoleteAttribute attribute and the Windows.Foundation.Metadata.DeprecatedAttribute attribute if only the ObsoleteAttribute is present in source code. 會轉換成,如下所示 ObsoleteAttribute DeprecatedAttributeThe ObsoleteAttribute is transformed to the DeprecatedAttribute as follows:

  • 如果 messageerror 引數都存在, message 則會指派給 DeprecatedAttribute message 引數。If the message and error arguments are both present, message is assigned to the DeprecatedAttribute message argument. 的錯誤值會 true 對應至 DeprecationType,而的值會 error false 對應至 DeprecationType. 取代An error value of true maps to DeprecationType.Remove, and an error value of false maps to DeprecationType.Deprecate.

  • 如果 message 未在中提供引數,則在 ObsoleteAttribute 中的預設值 DeprecatedAttribute 為「element_name 已被取代」,其中 element_name 是套用屬性之目的程式元素的名稱。If the message argument is not supplied in the ObsoleteAttribute, its default value in the DeprecatedAttribute is "element_name is deprecated", where element_name is the name of the target program element to which the attribute is applied.

  • 如果 error 引數不存在於中 ObsoleteAttribute ,則其在中的預設值 DeprecatedAttributeDeprecationType. 取代If the error argument is not present in the ObsoleteAttribute, its default value in the DeprecatedAttribute is DeprecationType.Deprecate.

重要

不建議直接將 >windows.foundation.deprecatedattribute 屬性套用至 managed 程式碼,因為這項匯出會自動發生。Directly applying the Windows.Foundation.Metadata.DeprecatedAttribute attribute to managed code is not recommended, because this export occurs automatically.

建構函式

ObsoleteAttribute()

使用預設屬性,初始化 ObsoleteAttribute 類別的新執行個體。Initializes a new instance of the ObsoleteAttribute class with default properties.

ObsoleteAttribute(String)

使用指定的運算訊息來初始化 ObsoleteAttribute 類別的新執行個體。Initializes a new instance of the ObsoleteAttribute class with a specified workaround message.

ObsoleteAttribute(String, Boolean)

使用運算訊息和指示使用已過時項目是否視為錯誤的布林值 (Boolean),來初始化 ObsoleteAttribute 類別的新執行個體。Initializes a new instance of the ObsoleteAttribute class with a workaround message and a Boolean value indicating whether the obsolete element usage is considered an error.

屬性

DiagnosticId

取得或設定報告使用 API 時,編譯器所用的識別碼。Gets or sets the ID that the compiler will use when reporting a use of the API.

IsError

取得值,其指出編譯器是否會將使用過時的程式元素視為錯誤。Gets a value that indicates whether the compiler will treat usage of the obsolete program element as an error.

Message

取得因應措施訊息。Gets the workaround message.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)
UrlFormat

取得或設定相對應文件的 URL。Gets or sets the URL for corresponding documentation. API 會接受格式字串而非實際 URL,並建立包含診斷識別碼的泛型 URL。The API accepts a format string instead of an actual URL, creating a generic URL that includes the diagnostic ID.

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱