ConditionalAttribute 클래스

정의

지정된 조건부 컴파일 기호가 정의되어 있지 않으면 메서드 호출 또는 특성이 무시되어야 함을 컴파일러에 알립니다.

public ref class ConditionalAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
public sealed class ConditionalAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=true)]
[System.Serializable]
public sealed class ConditionalAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ConditionalAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)>]
type ConditionalAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=true)>]
[<System.Serializable>]
type ConditionalAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ConditionalAttribute = class
    inherit Attribute
Public NotInheritable Class ConditionalAttribute
Inherits Attribute
상속
ConditionalAttribute
특성

예제

다음 예에서는 ConditionalAttribute의 사용법을 보여줍니다. 이 예제에서는 조건이 /define 컴파일러 옵션으로 정의되어 있다고 가정합니다. 컴파일러 옵션을 변경하여 다른 결과를 얻을 수 있습니다. 필요에 따라 컴파일러 옵션으로 식별하는 대신 샘플 코드에서 pragmas를 사용하여 조건을 정의할 수 있습니다.

#define CONDITION1
#define CONDITION2
using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
        Console.WriteLine("Calling Method1");
        Method1(3);
        Console.WriteLine("Calling Method2");
        Method2();

        Console.WriteLine("Using the Debug class");
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.WriteLine("DEBUG is defined");
    }

    [Conditional("CONDITION1")]
    public static void Method1(int x)
    {
        Console.WriteLine("CONDITION1 is defined");
    }

    [Conditional("CONDITION1"), Conditional("CONDITION2")]
    public static void Method2()
    {
        Console.WriteLine("CONDITION1 or CONDITION2 is defined");
    }
}

/*
When compiled as shown, the application (named ConsoleApp)
produces the following output.

Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or CONDITION2 is defined
Using the Debug class
DEBUG is defined
*/
#Const CONDITION1 = True
#Const CONDITION2 = True
Imports System.Diagnostics

Class Test

    Shared Sub Main()
        Console.WriteLine("Calling Method1")
        Method1(3)
        Console.WriteLine("Calling Method2")
        Method2()
        
        Console.WriteLine("Using the Debug class")
        Debug.Listeners.Add(New ConsoleTraceListener())
        Debug.WriteLine("DEBUG is defined")
    End Sub
       
    <ConditionalAttribute("CONDITION1")> _
    Shared Sub Method1(x As Integer)
        Console.WriteLine("CONDITION1 is defined")
    End Sub
    
    <ConditionalAttribute("CONDITION1"), ConditionalAttribute("CONDITION2")> _
    Shared Sub Method2()
        Console.WriteLine("CONDITION1 or CONDITIOIN2 is defined")
    End Sub
    
End Class


' When compiled as shown, the application (named ConsoleApp) 
' produces the following output.

'Calling Method1
'CONDITION1 is defined
'Calling Method2
'CONDITION1 or CONDITION2 is defined
'Using the Debug class
'DEBUG is defined

설명

메서드 및 클래스에 ConditionalAttribute 특성을 적용할 수 있습니다. 그러나 클래스에서의 사용은 파생 Attribute된 형식에만 유효합니다. ConditionalAttribute 은 무시되거나 다른 형식에 적용하는 경우 컴파일러 경고 또는 오류 메시지를 생성합니다.

메서드에 적용하면 ConditionalAttribute 연결된 조건부 컴파일 기호가 정의되지 않는 한 메서드에 대한 호출을 MSIL(Microsoft 중간 언어)로 컴파일해서는 안 된다는 것을 컴파일러에 ConditionalAttribute 나타냅니다. void를 반환하지 않는 메서드에 이 특성을 적용하면 Visual Studio 컴파일 오류가 발생합니다. 특성에 적용하면 ConditionalAttribute 조건부 컴파일 기호가 정의되지 않는 한 특성을 메타데이터에 내보내지 않아야 합니다. 메서드 또는 특성에 전달된 모든 인수는 컴파일러에서 여전히 형식을 검사합니다.

다음 기술을 사용하여 조건부 컴파일 기호를 정의할 수 있습니다.

  • 컴파일러 명령줄 옵션을 사용합니다. 예를 들어 /define:DEBUG입니다.

  • 운영 체제 셸에서 환경 변수를 사용합니다. 예를 들어 DEBUG=1을 설정합니다.

  • 소스 코드에서 pragma를 사용합니다. 예를 들어 다음과 같이 컴파일 변수를 정의합니다.

    #define DEBUG  
    
    #Const DEBUG=True  
    

    변수를 정의 취소하려면 다음을 사용합니다.

    #undef DEBUG  
    
    #Const DEBUG=False  
    

CLS(공용 언어 사양)를 준수하는 컴파일러는 무시 ConditionalAttribute하도록 허용됩니다. C#, F#, Visual Basic 및 C++ 컴파일러가 지원ConditionalAttribute합니다. JScript 컴파일러는 이 특성을 지원하지 않습니다.

참고

Visual Basic 연산자는 이 특성의 AddressOf 영향을 받지 않습니다. 예를 들어, Call CType(AddressOf delegate, Action) 항상 호출하지만delegate``Call delegate(), 그렇지 않을 수도 있습니다.

ConditionalAttribute는 및 Trace 클래스에 정의된 메서드에 Debug 적용됩니다.

특성을 사용 하는 방법에 대 한 자세한 내용은 참조 하세요. 특성합니다.

생성자

ConditionalAttribute(String)

ConditionalAttribute 클래스의 새 인스턴스를 초기화합니다.

속성

ConditionString

ConditionalAttribute 특성과 관련된 조건부 컴파일 기호를 가져옵니다.

TypeId

파생 클래스에서 구현된 경우 이 Attribute에 대한 고유 식별자를 가져옵니다.

(다음에서 상속됨 Attribute)

메서드

Equals(Object)

이 인스턴스가 지정된 개체와 같은지를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
GetHashCode()

이 인스턴스의 해시 코드를 반환합니다.

(다음에서 상속됨 Attribute)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
IsDefaultAttribute()

파생 클래스에서 재정의된 경우 이 인스턴스 값이 파생 클래스에 대한 기본값인지 여부를 표시합니다.

(다음에서 상속됨 Attribute)
Match(Object)

파생 클래스에서 재정의된 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다.

(다음에서 상속됨 Attribute)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

이름 집합을 해당하는 디스패치 식별자 집합에 매핑합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다.

(다음에서 상속됨 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

개체에서 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1).

(다음에서 상속됨 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

개체에서 노출하는 메서드와 속성에 대한 액세스를 제공합니다.

(다음에서 상속됨 Attribute)

적용 대상