See Also: ObsoleteAttribute Members
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.
The ObsoleteAttribute class includes two properties:
ObsoleteAttribute.Message. The string assigned to the ObsoleteAttribute.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.
ObsoleteAttribute.IsError. This is a Boolean value that indicates to the compiler whether using the ObsoleteAttribute attribute should cause it to emit an error (ObsoleteAttribute.IsError is true) or a warning (ObsoleteAttribute.IsError is false).
For more information about using attributes, see [<topic://cpconExtendingMetadataUsingAttributes>].
When you create a Windows Metadata library (.winmd file), the ObsoleteAttribute is exported as both the ObsoleteAttribute attribute and the tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecatedattribute.aspx attribute if only the ObsoleteAttribute is present in source code. The ObsoleteAttribute is transformed to the DeprecatedAttribute as follows:
If the message and error arguments are both present, message is assigned to the DeprecatedAttribute message argument. An error value of true maps to tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecationtype.aspx, and an error value of false maps to tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecationtype.aspx.
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.
If the error argument is not present in the ObsoleteAttribute, its default value in the DeprecatedAttribute is tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecationtype.aspx.
Directly applying the tp://msdn.microsoft.com/library/windows/apps/windows.foundation.metadata.deprecatedattribute.aspx attribute to managed code is not recommended, because this export occurs automatically.
The following example demonstrates the usage of ObsoleteAttribute to generate a compile-time warning.
C# Example
using System; public class ObsoleteAttributeExample { [ObsoleteAttribute("OldMethod is being removed: use NewMethod in future versions.")] public static void OldMethod() { //Execute some code here } public static void Main() { OldMethod(); } }
An example compile-time result is
ObsoleteAttributeExample.cs(8,4): warning CS0618: 'ObsoleteAttributeExample.OldMethod()' is obsolete: 'OldMethod is being removed: use NewMethod in future versions.'