public sealed class ConditionalAttribute : Attribute
Object
Attribute
ConditionalAttribute
mscorlib
BCL
Indicates to compilers that a method is callable if and only if a specified pre-processing identifier has been defined on the method.
[Note: A ConditionalAttribute, which has an associated condition System.Diagnostics.ConditionalAttribute.ConditionString, can be attached to the definition of a method, creating a conditionalmethod . Thereafter, when a compiler encounters a call to that method, it might choose to ignore the call unless a compilation variable is defined at the site of the call, with a value that matches in a case-sensitive manner the System.Diagnostics.ConditionalAttribute.ConditionString supplied to the ConditionalAttribute .Note that compilers might provide several techniques to define such compilation variables, such as:
CLS-Compliant compilers are permitted to ignore uses of the ConditionalAttribute .
- compiler command-line switches (for example,
/define:DEBUG
)
- environment variables in the operating system shell (for example,
SET DEBUG=1
)
- as pragmas in the source code (for example,
#define DEBUG
, to define the compilation variable, or#undef DEBUG
to undefine it)
]
The following example demonstrates the use of ConditionalAttribute with a particular compiler that supports the use of this attribute. The System.Diagnostics.ConditionalAttribute.ConditionString property of the current attribute is initialized as "DEBUG".
When this code is compiled with the compilation-variable DEBUG defined at the callsite, the output when run is
using System; using System.Diagnostics; public class MyClass { [ConditionalAttribute("DEBUG")] public static void Display() { Console.WriteLine("Compiled with DEBUG"); } } public class TestCondition { public static void Main() { Console.WriteLine("How was this compiled?"); MyClass.Display(); Console.WriteLine("<eop>"); } }
How was this compiled?
When this code is compiled without the compilation-variable DEBUG defined at the callsite, the output when run isCompiled with DEBUG
<eop>
How was this compiled?
<eop>
AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=true, Inherited=true)
System.Diagnostics Namespace
ConditionalAttribute Constructors
ConditionalAttribute Constructor
ConditionalAttribute Properties
public ConditionalAttribute(string conditionString);
Constructs and initializes a new instance of the ConditionalAttribute class.
- conditionString
- A String that contains the pre-processing identifier that makes callable the target method of the current instance.
This constructor initializes the System.Diagnostics.ConditionalAttribute.ConditionString property of the current instance using conditionString.
System.Diagnostics.ConditionalAttribute Class, System.Diagnostics Namespace
public string ConditionString { get; }
Gets the String that contains the pre-processing identifier that makes callable the target method of the current instance.
A String that contains the pre-processing identifier that makes callable the target method of the current instance.
This property is read-only.
System.Diagnostics.ConditionalAttribute Class, System.Diagnostics Namespace