C# 如何在运行时获取构建配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain build configuration at runtime?
提问by chugh97
Does anyone know how to get the current build configuration $(Configuration)
in C# code?
有谁知道如何$(Configuration)
在 C# 代码中获取当前的构建配置?
采纳答案by Binary Worrier
You can't, not really. What you can do is define some "Conditional Compilation Symbols", if you look at the "Build" page of you project settings, you can set these there, so you can write #if statements to test them.
你不能,不是真的。您可以做的是定义一些“条件编译符号”,如果您查看项目设置的“构建”页面,您可以在那里进行设置,以便您可以编写#if 语句来测试它们。
A DEBUG symbol is automatically injected (by default, this can be switched off) for debug builds.
调试符号会自动注入(默认情况下,可以关闭)用于调试版本。
So you can write code like this
所以你可以写这样的代码
#if DEBUG
RunMyDEBUGRoutine();
#else
RunMyRELEASERoutine();
#endif
However, don't do this unless you've good reason. An application that works with different behavior between debug and release builds is no good to anyone.
但是,除非您有充分的理由,否则不要这样做。在调试和发布版本之间以不同行为工作的应用程序对任何人都没有好处。
回答by Aaron Powell
I don't believe you can inject that at compile time into the assembly but one way you could achieve it would be to use MSBuild and add it to the config file of the application.
我不相信您可以在编译时将其注入程序集,但实现它的一种方法是使用 MSBuild 并将其添加到应用程序的配置文件中。
See this blog post about how to do multi-environment config files using MSBuild - http://adeneys.wordpress.com/2009/04/17/multi-environment-config/
请参阅有关如何使用 MSBuild 执行多环境配置文件的博客文章 - http://adeneys.wordpress.com/2009/04/17/multi-environment-config/
Alternatively you could write an MSBuild task which would edit a certain compiled file (your C# or VB file) and have that run in the BeforeBuild
task. It'd be rather tricky as you'd need to work out where to inject it into the file, but provided you had some kind of tokenization set up you should be able to do it. I also doubt it would be pretty!
或者,您可以编写一个 MSBuild 任务,该任务将编辑某个编译文件(您的 C# 或 VB 文件)并在BeforeBuild
任务中运行该文件。这会相当棘手,因为您需要确定将其注入文件的位置,但是如果您设置了某种标记化,您应该能够做到。我也怀疑它会漂亮!
回答by Vaccano
If you unload your project (in the right click menu) and add this just before the </Project>
tag it will save out a file that has your configuration in it. You could then read that back in for use in your code.
如果你卸载你的项目(在右键菜单中)并在</Project>
标签之前添加它,它将保存一个包含你的配置的文件。然后您可以将其读回以在您的代码中使用。
<Target Name="BeforeBuild">
<WriteLinesToFile File="$(OutputPath)\env.config"
Lines="$(Configuration)" Overwrite="true">
</WriteLinesToFile>
</Target>
回答by Jan Aagaard
Conditional Compilation Symbols can by used to achieve this. You can define custom symbols the Properties > Build settings pane for each project, and the use the #if directives to test them in the code.
可以使用条件编译符号来实现这一点。您可以在“属性”>“构建设置”窗格中为每个项目定义自定义符号,并使用 #if 指令在代码中测试它们。
Example showing how the define the symbol UNOEURO and how to use it in code.
示例显示如何定义符号 UNOEURO 以及如何在代码中使用它。
bool isUnoeuro = false;
#if UNOEURO
isUnoeuro = true;
#endif
回答by Jeeva Subburaj
You can use a common static method with Conditional Attribute to set the flag to detect DEBUG or RELEASE mode. The SetDebugMode method will be called only when running in the DEBUG mode otherwise it is ignored by Runtime.
您可以使用带有条件属性的通用静态方法来设置标志以检测 DEBUG 或 RELEASE 模式。只有在 DEBUG 模式下运行时才会调用 SetDebugMode 方法,否则它会被 Runtime 忽略。
public static class AppCompilationConfiguration
{
private static bool debugMode;
private static bool IsDebugMode()
{
SetDebugMode();
return debugMode;
}
//This method will be loaded only in the case of DEBUG mode.
//In RELEASE mode, all the calls to this method will be ignored by runtime.
[Conditional("DEBUG")]
private static void SetDebugMode()
{
debugMode = true;
}
public static string CompilationMode => IsDebugMode() ? "DEBUG" : "RELEASE";
}
You can call it in the code like below
您可以在如下代码中调用它
Console.WriteLine(AppCompilationConfiguration.CompilationMode);
回答by Jim G.
- Install the SlowCheetahVisual Studio extension.
- Right-click on your config file and select 'Add Transform'.
- 安装SlowCheetahVisual Studio 扩展。
- 右键单击您的配置文件并选择“添加转换”。
- Notice a transform for each build configuration.
- 注意每个构建配置的转换。
- Place a "Build" appSetting into the root config file:
- 将“Build” appSetting 放入根配置文件中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings>
<add key="Build" value="" />
</appSettings>
</configuration>
- And place a "Build" directive in each transform:
- 并在每个转换中放置一个“构建”指令:
<?xml version="1.0" encoding="utf-8"?>
<!--For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Build" value="Debug" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
</configuration>
- Then obtain the "Build" appSetting value in your C# code:
- 然后在您的 C# 代码中获取“Build”appSetting 值:
ConfigurationManager.AppSettings["Build"]
ConfigurationManager.AppSettings["Build"]
- Please be mindful that you will notsee the transforms when you are debugging: https://stackoverflow.com/a/17336009/109941
- 请注意,您在调试时不会看到转换:https: //stackoverflow.com/a/17336009/109941
回答by Egor Novikov
There is AssemblyConfigurationAttributein .NET. You can use it in order to get name of build configuration
.NET 中有AssemblyConfigurationAttribute。您可以使用它来获取构建配置的名称
var assemblyConfigurationAttribute = typeof(CLASS_NAME).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
var buildConfigurationName = assemblyConfigurationAttribute?.Configuration;