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.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft