C#条件编译选项:Conditional(代替#if...#endif)
2010-04-21 11:06
337 查看
#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 编译器不支持此属性。
#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 编译器不支持此属性。
相关文章推荐
- C#条件编译选项:Conditional(代替#if...#endif)
- C#条件编译选项:Conditional(代替#if...#endif)
- C#条件编译选项:Conditional(代替#if...#endif)
- 改善C#程序的50种方法 条款4:使用Conditional特性代替#if条件编译
- Effective C# Item4:使用Conditional特性代替#if条件编译
- 使用Conditional特性代替#if条件编译
- Effective c# Item4:使用Conditional特性代替#if条件编译
- 也说条件编译 #if #else #endif && Conditional Attribute
- 条款4:使用Conditional特性代替#if条件编译
- 提高C#编程水平的50个要点 之四“使用条件属性 (Conditional Attributes) 来代替条件编译语句 #if”
- Effective C# Item4:使用Conditional特性代替#if条件编译
- C# 使用Conditional特性而不是#if条件编译
- 条款4:使用Conditional特性代替#if条件编译
- 条款4:使用Conditional特性代替#if条件编译
- 条款4:使用Conditional特性代替#if条件编译
- 条件编译(#ifdef #else #endif #if)
- 编译中的一些条件分支 #if #else #endif
- C语言的条件编译 #if #elif #endif #ifndef #define
- C# 中的#if、#elif、#else、#endif等条件编译符号
- 条件判断语句(if...else)与条件编译(#if .... #else ... #endif)