C# 条件等效于 !DEBUG

标签 c# conditional debug-symbols

<分区>

什么是 C# System.Diagnostics.Conditional 等效于 #if (!DEBUG)

如果尚未在 DEBUG 模式下编译,我想加密控制台应用程序的 app.config 文件的一部分。这是这样实现的:

public static void Main(string[] args)
{
    #if (!DEBUG)
    ConfigEncryption.EncryptAppSettings();
    #endif
    //...
}

但不知何故,我更喜欢用条件属性装饰加密方法:

[Conditional("!DEBUG")]
internal static void EncryptAppSettings()
{
    //...
}

然而,这让编译器感到难过:“System.Diagnostics.ConditionalAttribute”属性的参数必须是有效标识符...

否定条件参数的正确语法是什么?

编辑: 感谢@Gusdor,我使用了这个(我更喜欢让 Program.cs 文件没有 if/else 调试逻辑):

#if !DEBUG
#define ENCRYPT_CONFIG
#endif

[Conditional("ENCRYPT_CONFIG")]
internal static void EncryptAppSettings()
{
    //...
}

最佳答案

使用该属性会有点麻烦,但可以做到。

#if DEBUG
//you have nothing to do here but c# requires it
#else
#define NOT_DEBUG //define a symbol specifying a non debug environment
#endif

[Conditional("NOT_DEBUG")]
internal static void EncryptAppSettings()
{
    //...
}

关于C# 条件等效于 !DEBUG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361149/

相关文章:

java - 在Java Eclipse中添加 "Run to Line"按钮

c++ - 抽象类使用shared_ptr时如何在nm或objdump中找到函数符号?

c# - Action(arg) 和 Action.Invoke(arg) 之间的区别

c# - 在 C# 整数运算中,a/b/c 是否总是等于 a/(b*c)?

c# - 在一个条件下比较多个值

php - 检查多个值是全部为假还是全部为真

elf - Trace32 命令从 ELF 文件中读取符号内容

c# - MVC5 Account Controller 空引用异常

c# - 使用正则表达式提取数据 url

amazon-web-services - Amazon S3 条件放置对象