다음을 통해 공유


ObsoleteAttribute.IsError 속성

정의

컴파일러에서 오래된 프로그램 요소를 사용하는 것을 오류로 처리할지 여부를 나타내는 값을 가져옵니다.

public:
 property bool IsError { bool get(); };
public bool IsError { get; }
member this.IsError : bool
Public ReadOnly Property IsError As Boolean

속성 값

Boolean

오래된 요소를 사용하는 것이 오류로 간주되면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

예제

다음 예제에서는 사용되지 않는 것으로 표시된 두 멤버를 포함하는 클래스를 정의합니다. 첫 번째 속성 OldProperty은 호출될 경우 컴파일러 경고를 생성합니다. 두 번째 메서드 CallOldMethod는 컴파일러 오류를 생성합니다. 이 예제에서는 리플렉션을 사용하여 형식의 멤버에 적용된 특성에 대한 ObsoleteAttribute 정보를 얻고 해당 특성과 IsError 속성의 Message 값을 표시합니다.

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

type Example() =
    // Mark OldProperty As Obsolete.
    [<ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)>]
    member _.OldProperty =
        "The old property value."

    member _.NewProperty =
        "The new property value."

    // Mark OldMethod As Obsolete.
    [<ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", true)>]
    member _.OldMethod() =
        "You have called OldMethod."

    member _.NewMethod() =
        "You have called NewMethod."

// Get all public members of this type.
let members = typeof<Example>.GetMembers()
// Count total obsolete members.
let mutable n = 0

// Try to get the ObsoleteAttribute for each public member.
printfn "Obsolete members in the Example class:\n"
for m in members do
    let attribs = m.GetCustomAttributes(typeof<ObsoleteAttribute>, false) |> box :?> ObsoleteAttribute[]
    if attribs.Length > 0 then
        let attrib = attribs[0]
        printfn $"Member Name: {m.DeclaringType.FullName}.{m.Name}"
        printfn $"   Message: {attrib.Message}"
        printfn $"""   Warning/Error: {if attrib.IsError then "Error" else "Warning"}"""
        n <- n + 1

if n = 0 then
    printfn "The Example type has no obsolete attributes."
// The example displays the following output:
//       Obsolete members in the Example class:
//
//       Member Name: Example+Example.OldMethod
//          Message: This method is obsolete. Call NewMethod instead.
//          Warning/Error: Error
//       Member Name: Example+Example.OldProperty
//          Message: This property is obsolete. Use NewProperty instead.
//          Warning/Error: Warning
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 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:")
      Console.WriteLine()
      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
      Next
      
      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

적용 대상