ObsoleteAttribute ObsoleteAttribute ObsoleteAttribute ObsoleteAttribute Class

定義

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

public ref class ObsoleteAttribute sealed : Attribute
[System.Runtime.InteropServices.ComVisible(true)]
[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
type ObsoleteAttribute = class
    inherit Attribute
Public NotInheritable Class ObsoleteAttribute
Inherits Attribute
繼承
ObsoleteAttributeObsoleteAttributeObsoleteAttributeObsoleteAttribute
屬性

範例

下列範例會定義一個類別,其中包含屬性和以ObsoleteAttribute屬性標記的方法。The 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 will be removed in future versions of the product.

ObsoleteAttribute類別包含兩個屬性:The ObsoleteAttribute class includes two properties:

  • Message.Message. 當屬性目標用於程式Message代碼時,編譯器會發出指派給屬性的字串。The string assigned to the Message property is emitted by the compiler when the attribute target is used in code. 字串應該要注意的是,屬性目標已過時,而且可能的話,請提供一些因應措施或以程式設計方式替代的方法。The string should note that the attribute target is obsolete and, if possible, provide some workaround or programmatic alternative.

  • IsError.IsError. 這是布林ObsoleteAttribute值,指出使用屬性是否應使其發出錯誤(IsError is true)或警告(IsErrorfalse)。This is a Boolean value that indicates 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存在ObsoleteAttribute ,則會同時匯出為屬性和DeprecatedAttribute屬性。When 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. 會轉換成, DeprecatedAttribute如下所示: ObsoleteAttributeThe ObsoleteAttribute is transformed to the DeprecatedAttribute as follows:

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

  • DeprecatedAttribute如果未 ObsoleteAttribute在中提供 引數,其預設值為 "element_name is 已淘汰",其中 element_name 是套用屬性之目的程式元素的名稱。 messageIf 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.

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

重要

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

建構函式

ObsoleteAttribute() ObsoleteAttribute() ObsoleteAttribute() ObsoleteAttribute()

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

ObsoleteAttribute(String) ObsoleteAttribute(String) ObsoleteAttribute(String) ObsoleteAttribute(String)

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

ObsoleteAttribute(String, Boolean) ObsoleteAttribute(String, Boolean) ObsoleteAttribute(String, Boolean) 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.

屬性

IsError IsError IsError IsError

取得指示編譯器 (Compiler) 是否將使用已過時程式項目視為錯誤的布林值。Gets a Boolean value indicating whether the compiler will treat usage of the obsolete program element as an error.

Message Message Message Message

取得運算訊息,包含其他程式項目的說明。Gets the workaround message, including a description of the alternative program elements.

TypeId TypeId TypeId TypeId

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

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

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

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

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

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)

明確介面實作

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

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

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

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

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

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

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

(Inherited from Attribute)

適用於

另請參閱