Conditional 编译

根据预处理标识符执行方法。Conditional 特性是 ConditionalAttribute 的别名,可应用于方法或继承自Attribute类的子类。如果应用在其他类上会产生编译错误。

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:

下面3种方式用来定义compilation symbols:

如下面的例子在文件的最开头定义了两个标志,如果把这两行注释掉,则用相应Conditional标记的方法就不会执行:
				

 

#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 或CONDITION2 的存在将导致方法调用:

 

    [Conditional("CONDITION1"), Conditional("Condition2")]  


				public
				static
				void Method2()

    {

        Console.WriteLine("CONDITION1 or Condition2 is defined");

    }

}

 

若要获得对符号进行逻辑""运算的效果,可以定义序列条件方法。例如,仅当 A B 均已定义时,才能执行下面的第二种方法:

[Conditional("A")]

static void DoIfA()

{

DoIfAandB();

}

 

[Conditional("B")]

static void DoIfAandB()

{

// Code to execute when both A and B are defined...

}

 

使用 Conditional 是封闭 #if #endif 内部方法的替代方法,它更整洁、更别致、减少了出错的机会,如下例所示:

#if DEBUG

void ConditionalMethod()

{

}

#endif

二者是同样的作用。

 

Debug上的大多数诊断函数需要当前项目定义了"DEBUG"预定义变量才能够使用。否则,将不执行任何操作。
Trace检查的预定义变量是"TRACE"。

默认情况下,项目的Debug配置会定义DEBUG和TRACE两个预定义常量。但是Release配置仅仅定义了TRACE常量。这就是为什么你发现Debug.WriteLine在Release模式下没有任何输出。
修改这些配置,可以通过Project -> XXXXX Properties -> Build -> Define DEBUG constant 启用和禁用DEBUG或TRACE常量。

 

 

Console.WriteLine和Debug.WriteLine的区别:

Console.WriteLine writes to the standard output stream, both in debug and release.

Debug.WriteLine uses the Debug.Listeners property and writes to every TraceListener installed. By default, this is just the output window, so it behaves similarly.

Debug.writeline is only for the purpose of debugging your application. In the release mode your debug statements will be ignored(not compiled to the code).

As Debug.WriteLine writes to all the trace listeners in the the Listeners collection, it is possible that this could be output in more than one place (VS output window, Console, Log file, 3rd party app which registers a listener).

If your purpose of using console.writeline is solely for debugging, you better use debug.writeline.

If you want to show a message to your user, you would use console.writeline.

 

 

 

 

原文地址:https://www.cnblogs.com/bear831204/p/2136301.html