Export (0) Print
Expand All

ConditionalAttribute Class

Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

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

[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true)] 
[ComVisibleAttribute(true)] 
public sealed class ConditionalAttribute : Attribute
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ConditionalAttribute extends Attribute
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple=true) 
ComVisibleAttribute(true) 
public final class ConditionalAttribute extends Attribute
Not applicable.

You can apply the ConditionalAttribute attribute to methods and classes. However, its use on classes is valid only for types that are derived from Attribute. ConditionalAttribute either will be ignored or will produce a compiler warning or error message if you apply it to any other type.

NoteNote:

In the .NET Framework versions 1.0 and 1.1, the ConditionalAttribute attribute can be applied only to methods.

Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined. Applying ConditionalAttribute to an attribute indicates that the attribute should not be emitted to metadata unless the conditional compilation symbol is defined. Any arguments passed to the method or attribute are still type-checked by the compiler.

You can use the following techniques to define conditional compilation symbols:

  • Compiler command-line options (for example, /define:DEBUG).

  • Environment variables in the operating system shell (for example, set DEBUG=1).

  • Pragmas in the source code (for example, #define DEBUG to define the compilation variable and #undef DEBUG to undefine it).

Compilers that comply with the Common Language Specification (CLS) are permitted to ignore ConditionalAttribute. The C#, J#, and Visual Basic compilers support ConditionalAttribute; the C++ and JScript compilers do not support the attribute.

ConditionalAttribute is applied to the methods that are defined in the Debug and Trace classes.

For more information about how to use attributes, see Extending Metadata Using Attributes.

The following code example demonstrates the use of ConditionalAttribute. The example assumes that the condition is defined in the /define compiler option. You can obtain different results by changing the compiler option. You can optionally define the conditions in the sample code instead of identifying them as compiler options.

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.

csc ConsoleApp.cs
Calling Method1
Calling Method2
Using the Debug class

csc /define:CONDITION1 ConsoleApp.cs
Calling Method1
CONDITION1 is defined
Calling Method2
CONDITION1 or Condition2 is defined
Using the Debug class

csc /define:Condition2 ConsoleApp.cs
Calling Method1
Calling Method2
CONDITION1 or Condition2 is defined
Using the Debug class

csc /define:DEBUG ConsoleApp.cs
Calling Method1
Calling Method2
Using the Debug class
DEBUG is defined
*/

System.Object
   System.Attribute
    System.Diagnostics.ConditionalAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft