System.Diagnostics.ConditionalAttribute Class

public sealed class ConditionalAttribute : Attribute

Base Types

Object
  Attribute
    ConditionalAttribute

Assembly

mscorlib

Library

BCL

Summary

Indicates to compilers that a method is callable if and only if a specified pre-processing identifier has been defined on the method.

Description

[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 .

]

Example

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".

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>"); 
  }
}
      
When this code is compiled with the compilation-variable DEBUG defined at the callsite, the output when run is

How was this compiled?

Compiled with DEBUG

&lt;eop>

When this code is compiled without the compilation-variable DEBUG defined at the callsite, the output when run is

How was this compiled?

&lt;eop>

Attributes

AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple=true, Inherited=true)

See Also

System.Diagnostics Namespace

Members

ConditionalAttribute Constructors

ConditionalAttribute Constructor

ConditionalAttribute Properties

ConditionalAttribute.ConditionString Property


ConditionalAttribute Constructor

public ConditionalAttribute(string conditionString);

Summary

Constructs and initializes a new instance of the ConditionalAttribute class.

Parameters

conditionString
A String that contains the pre-processing identifier that makes callable the target method of the current instance.

Description

This constructor initializes the System.Diagnostics.ConditionalAttribute.ConditionString property of the current instance using conditionString.

See Also

System.Diagnostics.ConditionalAttribute Class, System.Diagnostics Namespace

ConditionalAttribute.ConditionString Property

public string ConditionString { get; }

Summary

Gets the String that contains the pre-processing identifier that makes callable the target method of the current instance.

Property Value

A String that contains the pre-processing identifier that makes callable the target method of the current instance.

Description

This property is read-only.

See Also

System.Diagnostics.ConditionalAttribute Class, System.Diagnostics Namespace