gre*_*ade 5 c# conditional debug-symbols
什么是 C#System.Diagnostics.Conditional
等价物#if (!DEBUG)
?
如果控制台应用程序没有在调试模式下编译,我想加密它的 app.config 文件的一部分。这是这样实现的:
public static void Main(string[] args)
{
#if (!DEBUG)
ConfigEncryption.EncryptAppSettings();
#endif
//...
}
Run Code Online (Sandbox Code Playgroud)
但不知何故,我更喜欢用条件属性装饰 encrypt 方法:
[Conditional("!DEBUG")]
internal static void EncryptAppSettings()
{
//...
}
Run Code Online (Sandbox Code Playgroud)
然而这让编译器很伤心: The argument to the 'System.Diagnostics.ConditionalAttribute' attribute must be a valid identifier...
否定条件参数的正确语法是什么?
编辑: 感谢@Gusdor,我使用了这个(我更喜欢让 Program.cs 文件没有 if/else 调试逻辑):
#if !DEBUG
#define ENCRYPT_CONFIG
#endif
[Conditional("ENCRYPT_CONFIG")]
internal static void EncryptAppSettings()
{
//...
}
Run Code Online (Sandbox Code Playgroud)
使用该属性会有点麻烦,但可以做到。
#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()
{
//...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2172 次 |
最近记录: |