C#條件編譯選項:Conditional(代替#if...#endif)
#if (Debug && Trace)
#define DebugAndTrace
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Print1();
Print2();
Print3();
}
[Conditional("DEBUG")]
static void Print1()
{
Console.WriteLine("You defined the Debug parameter");
}
//定義了Debug或者Trace後才會執行
//或者的關係
[Conditional("Debug"), Conditional("Trace")]
static void Print2()
{
Console.WriteLine("You defined the Debug or Trace parameter");
}
//只有定義了Debug和Trace後才會執行此方法
[Conditional("DebugAndTrace")]
static void Print3()
{
Console.WriteLine("You defined the Debug and Trace parameter");
}
}
}
From MSDN:http://msdn.microsoft.com/zh-cn/library/system.diagnostics.conditionalattribute.aspx
可以使用下面的技術來定義條件編譯符號:
編譯器命令列選項(例如,/define:DEBUG),
可以在VS2008的IDE進行設定, Project Property--->Build--->Conditional compilation symbols(如果多個科研使用逗號隔開)
系統預設設定了:DEBUG 和 TRACE
作業系統外殼程式中的環境變數(例如,set DEBUG=1)。
原始碼中的雜注(例如,用於定義編譯變數的 #define DEBUG,以及用於取消定義它的 #undef DEBUG)。
必須在file的最前方設定,#define debug或者組合操作
#if (Debug && Trace)
#define DebugAndTrace
#endif
允許符合公共語言規範 (CLS) 的編譯器忽略 ConditionalAttribute。C#、J# 和 Visual Basic 編譯器支援 ConditionalAttribute;C++ 和 JScript 編譯器不支援此屬性。