.NET/C# 項目如何優雅地設置條件編譯符號?


 

條件編譯符號指的是 Conditional Compilation Symbols。你可以在 Visual Studio 的項目屬性中設置,也可以直接在項目文件中寫入 DefineConstants 屬性。

不過對於不同種類的項目,我建議使用不同的設置方法。本文將介紹如何設置條件編譯符。


對於新舊格式的差別或者遷移,可以查看我的其他博客:

 

 

新格式推薦:在 csproj 文件中設置

在項目中設置 <DefineConstants /> 屬性:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.1;net47</TargetFrameworks>
    <DefineConstants>$(DefineConstants);WALTERLV</DefineConstants>
  </PropertyGroup>

</Project>

這里我使用字符串拼接的方式 $(DefineConstants);WALTERLV 來設置,這樣可以把預設的那些條件編譯符號保留,比如通常 Visual Studio 會幫你生成的 TRACE 條件編譯符。

但即便你不做這種拼接也不用擔心。因為基於框架或平台的條件編譯符號是自動設置的。例如 NETCOREAPP2_1 等都是在你指定 DefineConstants 之后自動設置的。以下是 Microsoft.NET.Sdk 中的部分源碼,可以證明這一點:

<PropertyGroup Condition="'$(DisableImplicitConfigurationDefines)' != 'true'">
  <ImplicitConfigurationDefine>$(Configuration.ToUpperInvariant())</ImplicitConfigurationDefine>
  
  <!-- Replace dashes and periods in the configuration with underscores. This makes it more likely that the resulting compilation constant will be a valid C# conditional compilation symbol. As the set of characters that aren't allowed is essentially open-ended, there's probably not a good way to fully sanitize the Configuration in MSBuild evaluation. If the resulting string still isn't a valid conditional combilation symbol, then the compiler will generate the following error and the define will be ignored: warning MSB3052: The parameter to the compiler is invalid, '/define:0BAD_DEFINE' will be ignored. -->
  
  <ImplicitConfigurationDefine>$(ImplicitConfigurationDefine.Replace('-', '_'))</ImplicitConfigurationDefine>
  <ImplicitConfigurationDefine>$(ImplicitConfigurationDefine.Replace('.', '_'))</ImplicitConfigurationDefine>
  <DefineConstants>$(DefineConstants);$(ImplicitConfigurationDefine)</DefineConstants>
</PropertyGroup>
<PropertyGroup>
  <DefineConstants>$(DefineConstants);$(ImplicitFrameworkDefine)</DefineConstants>
</PropertyGroup>

舊格式推薦:在 Visual Studio 項目屬性中設置

你可以在項目屬性的“生成”頁中找到條件編譯符號的設置。

我自己用的 Visual Studio 是英文版的,但是也感謝小伙伴 林德熙 幫我截了一張中文版的圖。

Conditional Compilation Symbols

條件編譯符號

你需要特別注意:

  • 設置條件編譯符號需要在各種配置下都設置,因為各種配置都是不一樣的;具體來說是 Debug 下要設,Release 下也要設,x86 下要設,x64 下也要設。

關於配置(Configuration)和條件編譯符號(Conditional Compilation Symbols)

你可能在你的代碼中同時看到 Pascal 命名規則的 Debug 和全部大寫的 DEBUG,或者看到 Release 和 RELEASE。這是兩個不同的概念。

Debug 和 Release 的名稱來自於配置(Configuration)。你的項目有 Debug 配置和 Release 配置,或者你自己定義的其他配置。你的項目編譯過程默認根據 Debug 和 Release 配置做了很多不同的編譯選項。例如 Debug 下會禁用優化而 Release 下會開啟優化。

而 DEBUG 和 RELEASE 這樣的全大寫名稱來自於條件編譯符號(Conditional Compilation Symbols),是真正在 C# 代碼中使用的符號。而這全大寫符號的定義是分別在 Debug 和 Release 配置下設置了不同的值來實現的。

所以這兩個是不同的概念,不要弄混淆了。

同時這也帶來了一些命名建議:

  1. 條件編譯符號使用全大寫命名
    • 例如:DEBUG, RELEASE, NET47, NETCOREAPP2_1
  2. 配置使用 Pascal 命名
    • 例如:Debug, Release

我的博客會首發於 https://walterlv.com/,而 CSDN 和博客園僅從其中摘選發布,而且一旦發布了就不再更新。

知識共享許可協議

本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名呂毅(包含鏈接:https://blog.csdn.net/wpwalter),不得用於商業目的,基於本文修改后的作品務必以相同的許可發布。如有任何疑問,請與我聯系


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM