.NET/C# 项目如何优雅地设置条件编译符号?

栏目: ASP.NET · 发布时间: 5年前

内容简介:条件编译符号指的是 Conditional Compilation Symbols。你可以在 Visual Studio 的项目属性中设置,也可以直接在项目文件中写入不过对于不同种类的项目,我建议使用不同的设置方法。本文将介绍如何设置条件编译符。对于新旧格式的差别或者迁移,可以查看我的其他博客:

条件编译符号指的是 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 是英文版的,但是也感谢小伙伴 林德熙 帮我截了一张中文版的图。

.NET/C# 项目如何优雅地设置条件编译符号?

.NET/C# 项目如何优雅地设置条件编译符号?

本文会经常更新,请阅读原文: https://walterlv.com/post/how-to-define-preprocessor-symbols.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

.NET/C# 项目如何优雅地设置条件编译符号? 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接:https://walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 (walter.lv@qq.com)


以上所述就是小编给大家介绍的《.NET/C# 项目如何优雅地设置条件编译符号?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Graph Theory

Introduction to Graph Theory

Douglas B. West / Prentice Hall / 2000-9-1 / USD 140.00

For undergraduate or graduate courses in Graph Theory in departments of mathematics or computer science. This text offers a comprehensive and coherent introduction to the fundamental topics of graph ......一起来看看 《Introduction to Graph Theory》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具