2

我有一个变量(即bool releaseMode = false;)我希望根据我们是否处于发布模式(releaseMode = true;)设置变量的值,否则为调试模式(releaseMode = false;

4

1 回答 1

2

根据您的问题,您可以使用它:

/// <summary>
/// Indicate if the executable has been generated in debug mode.
/// </summary>
static public bool IsDebugExecutable
{
  get
  {
    bool isDebug = false;
    CheckDebugExecutable(ref isDebug);
    return isDebug;
  }
}

[Conditional("DEBUG")]
static private void CheckDebugExecutable(ref bool isDebug)
  => isDebug = true;

当然,您可以将名称交换为:

IsReleaseExecutable

return !isDebug;

这种方法意味着所有代码都被编译。因此,任何代码都可以根据这个标志以及与用户或程序有关的任何其他行为参数来执行,例如调试和跟踪引擎的激活或停用。例如:

if ( IsDebugExecutable || UserWantDebug )  DoThat();

否则像这样的预处理器指令:

用于调试与发布的 C# if/then 指令

#if DEBUG vs. Conditional("DEBUG")

于 2021-06-04T08:29:09.330 回答