• VS中使用define和undef实现条件编译(csc.exe条件编译)


    在C和C++中常有用到宏定义#define,在C#中也这个关键字,不过功能不同了,C#中是作为条件编译的条件。

    #define XXX 是定义XXX为条件编译的条件,相当于将XXX设为true。

    #undef XXX是取消XXX的条件编译条件,相当于将XXX设为false。

    #define和#undef必须在using之前进行定义,不然编译不通过。#define和#undef需要结合#if/#elif/#endif一起使用,下面是一段示例代码

    #define DEGUB
    #undef TRACE
    //#define TRACE
    //#undef DEBUG
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter5_defineUndef
    {
        class Program
        {
            static void Main(string[] args)
            {
    #if DEBUG
                Console.WriteLine("Debug is enabled!");   
    #endif
      
               
    #if TRACE
                 Console.WriteLine("Trace is enabled!");
    #endif
                Console.ReadLine();
            }
        }
    }
    
    #define DEUBG 是将DEBUG设为true,#undef TRACE 是将TRACE设为false.所以输出的是Debug is enabled!.

    如果将#define DEBUG和#undef TRACE屏蔽,将#define TRACE和#undef DEBUG取消注释,那么将输出Trace is enabled!

    条件编译在测试和生成不同的版本时尤其有用。

    可参看该文章http://msdn.microsoft.com/en-us/library/yt3yck0x.aspx

    除了在IDE中指定条件编译的条件外,如果使用csc.exe来编译时,也可直接指定。不过,这时建议在代码中将#define和#undef的定义取消掉。修改部分的代码如下

    //#define DEGUB
    //#undef TRACE
    //#define TRACE
    //#undef DEBUG

    这时我们需要设到VS的CMD命令工具,然后执行csc.exe,如下图


    对于没有指定的条件,默认为false。



  • 相关阅读:
    web前端网站收藏
    wordpress安装(ubuntu+nginx+php+mariadb)
    硬盘分区表知识——详解硬盘MBR
    useradd添加用户
    闭包closure this
    什么是同步加载与异步加载
    css 两个span标签在同一行,高度不一样
    CSS label之间存在间距
    JS中如何跳出循环/结束遍历
    el-checkbox-group 无法选中
  • 原文地址:https://www.cnblogs.com/sparkleDai/p/7605060.html
Copyright © 2020-2023  润新知