【问题标题】:Alter configuration settings based on compiler constants根据编译器常量更改配置设置
【发布时间】:2016-10-14 02:01:33
【问题描述】:

在代码中,我知道根据编译器常量反映的当前活动构建配置来包含或排除某些代码部分,例如DEBUG

static void Main()
{
    #if DEBUG

        //While debugging this section is used.

    #else

        //In Release this section is used. This is the "normal" way.

    #endif 
}

现在我想在配置文件中做同样的事情,例如 web.config 或 app.config,如下所示:

<appSettings>
<!-- IF DEBUG -->
    <add key="foo" value="debug-setting" />
<!-- ELSE -->
    <add key="foo" value="release-setting" />
<!-- ENDIF -->
</appSettings>

我该怎么做?

【问题讨论】:

  • “这种设置”是什么意思? Easier way to debug a C# Windows Service不回答你的问题吗?
  • 我的意思是检查项目是处于调试模式还是发布模式,我在上面明确提到过。
  • 重复你所说的并不能使你的问题更清楚。你的意思是你想要&lt;if debug&gt;&lt;some config&gt;&lt;else&gt;&lt;some other config&lt;/endif&gt; 在你的app.config 中?你想use configuration transformation for that
  • 感谢您的回答(也许它会起作用)并感谢您减少我的问题,但并非世界上所有的开发人员都是一样的。
  • 我的意思是你的问题不清楚。你可能很清楚你想要什么,但是写出来让别人也明白这很难。我已尝试编辑您的问题,以显示我认为您的意思,并回答了该问题。

标签: c# debugging web-config app-config


【解决方案1】:

你不能这样做,配置不提供确切的功能。

您可以使用的是配置转换。使用它,您可以为每个构建配置定义一个可选的转换,您可以在其中执行以下操作:

App.config:

<appSettings>
    <add key="foo" value="debug-setting" />
</appSettings>

App.Release.config:

<appSettings>
    <add key="foo" value="release-setting" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

另见App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多