繁体   English   中英

可以在代码文件之外使用预处理程序指令吗?

[英]Can preprocessor directives be used outside of code files?

C#预处理程序指令可以用于除代码文件之外的其他指令吗?

我知道如何在代码文件中使用它们,这是MSDN-Pages中的示例:

// preprocessor_if.cs
#define DEBUG
#define MYTEST
using System;
public class MyClass 
{
    static void Main() 
    {
#if (DEBUG && !MYTEST)
        Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && MYTEST)
        Console.WriteLine("MYTEST is defined");
#elif (DEBUG && MYTEST)
        Console.WriteLine("DEBUG and MYTEST are defined");
#else
        Console.WriteLine("DEBUG and MYTEST are not defined");
#endif
    }
}

是否可以在XML(或其他非代码)文件中使用#if来调试和发布不同的数据? 例如:

<coustomXml>
#if DEBUG
    <item>debugdata</item>
#else
    <item>releaseData</item>
#endif
</coustomXml>

我用我的项目的app.config尝试了这个,但是编译器忽略了这些标志。

使用编译器指令是不可能的,但您可以根据构建类型使用config transofrmations:

http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM