Bin*_*ier 16
你不能,不是真的.您可以做的是定义一些"条件编译符号",如果查看项目设置的"构建"页面,可以在那里设置它们,这样就可以编写#if语句来测试它们.
对于调试版本,会自动注入DEBUG符号(默认情况下,可以关闭它).
所以你可以编写这样的代码
#if DEBUG
RunMyDEBUGRoutine();
#else
RunMyRELEASERoutine();
#endif
Run Code Online (Sandbox Code Playgroud)
但是,除非你有充分的理由,否则不要这样做.在调试和发布版本之间使用不同行为的应用程序对任何人都没有好处.
Vac*_*ano 16
如果您卸载项目(在右键菜单中)并在</Project>标记之前添加它,它将保存一个包含您配置的文件.然后,您可以将其重新读入以便在代码中使用.
<Target Name="BeforeBuild">
<WriteLinesToFile File="$(OutputPath)\env.config"
Lines="$(Configuration)" Overwrite="true">
</WriteLinesToFile>
</Target>
Run Code Online (Sandbox Code Playgroud)
条件编译符号可以用来实现这一点.您可以为每个项目的"属性">"构建设置"窗格定义自定义符号,并使用#if指令在代码中测试它们.
示例显示如何定义符号UNOEURO以及如何在代码中使用它.

bool isUnoeuro = false;
#if UNOEURO
isUnoeuro = true;
#endif
Run Code Online (Sandbox Code Playgroud)
小智 6
.NET 中有AssemblyConfigurationAttribute。您可以使用它来获取构建配置的名称
var assemblyConfigurationAttribute = typeof(CLASS_NAME).Assembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
var buildConfigurationName = assemblyConfigurationAttribute?.Configuration;
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
ConfigurationManager.AppSettings["Build"]
| 归档时间: |
|
| 查看次数: |
11946 次 |
| 最近记录: |