ObsoleteAttribute 类

定义

标记不再使用的程序元素。 此类不能被继承。

public ref class ObsoleteAttribute sealed : 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
[<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
属性

示例

以下示例定义一个类,该类包含属性和用 ObsoleteAttribute 特性标记的方法。 在代码中访问 属性的值 OldProperty 会生成编译器警告,但调用 CallOldMethod 方法会生成编译器错误。 该示例还显示了尝试编译源代码时生成的输出。

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.'
open System

// Mark oldValue As Obsolete.
[<ObsoleteAttribute("This value is obsolete. Use newValue instead.", false)>]
let oldValue =
    "The old property value."

let newValue =
    "The new property value."

// Mark callOldFunction As Obsolete.
[<ObsoleteAttribute("This function is obsolete. Call callNewFunction instead.", true)>]
let callOldFunction () =
    "You have called CallOldMethod."

let callNewFunction () =
    "You have called CallNewMethod."

printfn $"{oldValue}"
printfn ""
printfn $"{callOldFunction ()}"
// The attempt to compile this example produces output like the following output:
//    Example.fs(23,12): error FS0101: This construct is deprecated. This function is obsolete. Call callNewFunction instead.
//    Example.fs(21,12): warning FS0044: This construct is deprecated. This value is obsolete. Use newValue 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 适用于所有程序元素,程序集、模块、参数和返回值除外。 将元素标记为已过时会通知用户,该元素可能会在产品的未来版本中被删除。

在代码中使用属性目标时,编译器会发出分配给 Message 属性的字符串。 理想情况下,字符串应提供某种解决方法或编程替代方法。

IsError使用 属性向编译器指示使用 ObsoleteAttribute 特性是否应在 (IsErrortrue) 或) 警告 (IsErrorfalse 发出错误。

有关使用特性的详细信息,请参阅 特性

构造函数

ObsoleteAttribute()

使用默认属性初始化 ObsoleteAttribute 类的新实例。

ObsoleteAttribute(String)

使用指定的变通方法消息初始化 ObsoleteAttribute 类的新实例。

ObsoleteAttribute(String, Boolean)

使用变通方法消息和布尔值初始化 ObsoleteAttribute 类的新实例,该布尔值指示是否将使用已过时的元素视为错误。

属性

DiagnosticId

获取或设置当编译器报告 API 的使用情况时将使用的 ID。

IsError

获取指示编译器是否将使用已过时的程序元素视为错误的值。

Message

获取变通方法消息。

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)
UrlFormat

获取或设置相应文档的 URL。 API 接受格式字符串,而不是实际的 URL,进而创建包含诊断 ID 的通用 URL。

方法

Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅