ObsoleteAttribute.Message Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the workaround message, including a description of the alternative program elements.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public ReadOnly Property Message As String

Property Value

Type: System.String

The workaround text string.

The following example defines a class that contains two members marked as obsolete. The first, a property named OldProperty, produces a compiler warning if it is called. The second, a method named CallOldMethod, produces a compiler error. The example uses reflection to get information about the ObsoleteAttribute attributes applied to members of the type and displays the values of their Message and IsError properties.

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
         Return "The old property value."
      End Get
   End Property

   Public ReadOnly Property NewProperty As String
         Return "The new property value."
      End Get
   End Property

   ' Mark OldMethod As Obsolete.
   <ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", True)> 
   Public Function OldMethod() As String
      Return "You have called OldMethod."
   End Function

   Public Function NewMethod() As String   
      Return "You have called NewMethod."
   End Function   

   Public Sub Main()
      ' Get all public members of this type.
      Dim members() As MemberInfo = GetType(Example).GetMembers()
      ' Count total obsolete members.
      Dim n As Integer = 0

      ' Try to get the ObsoleteAttribute for each public member.
      Console.WriteLine("Obsolete members in the Example class:")
      For Each member In members
         Dim attribs() As ObsoleteAttribute = CType(member.GetCustomAttributes(GetType(ObsoleteAttribute), 
                                                                             False), ObsoleteAttribute())
         If attribs.Length > 0 Then
            Dim attrib As ObsoleteAttribute = attribs(0)
            Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name)
            Console.WriteLine("   Message: {0}", attrib.Message)             
            Console.WriteLine("   Warning/Error: {0}", if(attrib.IsError, "Error", "Warning"))      
            n += 1
         End If

      If n = 0 Then
         Console.WriteLine("The Example type has no obsolete attributes.")
      End If 
   End Sub  
End Module
' The example displays the following output:
'       Obsolete members in the Example class:
'       Member Name: Example.OldMethod
'          Message: This method is obsolete. Call NewMethod instead.
'          Warning/Error: Error
'       Member Name: Example.OldProperty
'          Message: This property is obsolete. Use NewProperty instead.
'          Warning/Error: Warning

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top