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

当你在 ( winmd 文件) 创建 Windows 元数据库时, ObsoleteAttribute ObsoleteAttribute 如果Windows.Foundation.Metadata.DeprecatedAttribute只有 ObsoleteAttribute 在源代码中存在,则将导出为属性和 windows.foundation.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. 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的错误值,并将 errorfalse 映射到 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 特性应用于托管代码,因为这种导出是自动进行的。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)

使用变通方法消息和布尔值初始化 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 的使用情况时将使用的 ID。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,进而创建包含诊断 ID 的通用 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)

将一组名称映射为对应的一组调度标识符。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)

适用于

另请参阅