Export (0) Print
Expand All

ObsoleteAttribute.Message Property

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

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

public string Message { get; }

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.

using System;
using System.Reflection;

public class Example
{
   // Mark OldProperty As Obsolete.
   [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
   public string OldProperty
   { get { return "The old property value."; } }

   public string NewProperty
   { get { return "The new property value."; } }

   // Mark OldMethod As Obsolete.
   [ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)] 
   public string OldMethod()
   {
      return "You have called OldMethod.";
   }

   public string NewMethod() 
   {   
      return "You have called NewMethod.";
   }   

   public static void Main()
   {                 
      // Get all public members of this type.
      MemberInfo[] members = typeof(Example).GetMembers();
      // Count total obsolete members. 
      int n = 0;

      // Try to get the ObsoleteAttribute for each public member.
      Console.WriteLine("Obsolete members in the Example class:\n");
      foreach (var member in members) {
         ObsoleteAttribute[] attribs = (ObsoleteAttribute[]) 
                                        member.GetCustomAttributes(typeof(ObsoleteAttribute), 
                                                                   false);
         if (attribs.Length > 0) {
            ObsoleteAttribute attrib = attribs[0];
            Console.WriteLine("Member Name: {0}.{1}", member.DeclaringType.FullName, member.Name);
            Console.WriteLine("   Message: {0}", attrib.Message);             
            Console.WriteLine("   Warning/Error: {0}", attrib.IsError ? "Error" : "Warning");      
            n++;
         }
      }

      if (n == 0)
         Console.WriteLine("The Example type has no obsolete attributes.");
   } 

}
// 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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft