ObsoleteAttribute 类

定义

标记不再使用的程序元素。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
继承
ObsoleteAttribute
属性

示例

下面的示例定义了一个类,该类包含一个属性和一个用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:

  • MessageMessage. 当在代码中使用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.

  • IsErrorIsError. 这是一个布尔值,指示编译器是否使用ObsoleteAttribute特性应导致其发出错误(IsErrortrue)或警告(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 ,则将导出为属性和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 转换DeprecatedAttribute为,如下所示:The 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的错误值, error false并将值映射到DeprecationType。弃用An error value of true maps to DeprecationType.Remove, and an error value of false maps to DeprecationType.Deprecate.

  • DeprecatedAttribute 如果未message ObsoleteAttribute在中提供参数,则中其默认值为 "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.

  • 如果中不DeprecatedAttribute存在error参数,则其默认值为ObsoleteAttribute DeprecationType. 弃用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.

属性

IsError

获取指示编译器是否将使用已过时的程序元素视为错误的布尔值。Gets a Boolean value indicating whether the compiler will treat usage of the obsolete program element as an error.

Message

获取变通方法消息,包括对可选程序元素的说明。Gets the workaround message, including a description of the alternative program elements.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

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)

适用于

另请参阅