gpt4 book ai didi

c# - 在 Visual Studio 中使用自定义构建配置

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:46 25 4
gpt4 key购买 nike

在我的客户端应用程序中,我有时会连接到 localhost:1242\SomeService.asmx,有时它会连接到 someDomain:1242\SomeService.asmx。换句话说,有时我想在本地进行测试,有时又想远程进行测试。

VS 为您提供的默认选项是调试和发布。我想创建自定义的,事实上我刚刚创建了一个新的构建配置: enter image description here

无论如何,如果我正在使用该配置,我如何在代码中知道?

我想做这样的事情:

if(Configuration.Type == ConfigTypes.Local)
ConectionString = "localhost:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote1)
ConectionString = "SomeDomain1:1242:\SomeService.asmx";
else if (Configuration.Type == ConfigTypes.Remote2)
ConectionString = "SomeDifDomain:1242:\SomeService.asmx";

Release模式也往往更高效?我将如何指定这些设置?

最佳答案

您可以定义条件编译符号。
项目属性 → 构建选项卡 → 条件编译符号
为不同的配置定义不同的符号,例如:

  • SRV_LOCAL 在“本地”配置中;
  • SRV_REMOTE1 在“Remote1”配置中;
  • SRV_REMOTE2 在“Remote2”配置中。

然后在代码中:

#if SRV_LOCAL
private const string SERVER = "localhost";
#elseif SRV_REMOTE1
private const string SERVER = "SomeDomain1";
#elseif SRV_REMOTE2
private const string SERVER = "SomeDifDomain";
#endif

关于c# - 在 Visual Studio 中使用自定义构建配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com