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 编译器不支持此属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值