当没有定义DEBUG符号时,等效于Debug.Assert()?

use*_*503 2 c# assert

我正在寻找将测试表达式的语法,如果结果为false DEBUG并且缺少符号,则抛出异常.但不是在那里.

我知道我可以用:

#if !DEBUG
  Trace.Assert(condition);
#endif
Run Code Online (Sandbox Code Playgroud)

而且我知道我可以使用:

#if !DEBUG
  SomeGlobal.Production = true;
#endif
Run Code Online (Sandbox Code Playgroud)

所以我可以写:

Trace.Assert(SomeGlobal.Production && condition);
Run Code Online (Sandbox Code Playgroud)

避免在不同的地方有编译说明.

还有其他方法吗?

Sim*_*hid 5

[Conditional("RELEASE")]
public static void AssertRelease(bool condition)
{
    Trace.Assert(condition);
}
Run Code Online (Sandbox Code Playgroud)

并确保在Release配置中定义"RELEASE",


ConditionalAttribute是一个很好的方法.

向编译器指示应忽略方法调用或属性,除非定义了指定的条件编译符号.

就像Debug.Assert一样,如果不满足条件,编译器会忽略对此方法的调用.