c# - 如何检查 csproj 中的条件编译符号

标签 c# csproj conditional-compilation

出于安全原因,我需要从生成的 exe 中排除一些代码和一些文件。我为此使用条件编译符号 SECURE 。 排除代码很简单

#if !SECURE
// Some secure code
#endif

但对于文件来说 - 没有那么多。仅当定义了 SECURE 符号时,我应该在下面的 Condition 中编写什么内容才能排除这些文件?

  <ItemGroup Condition="???">
    <Compile Remove="SecureClass.cs" />
    <None Include="SecureClass.cs" />
  </ItemGroup>

最佳答案

经过一番挖掘,我发现 this github issue .

所以答案是:

  <ItemGroup Condition="$(DefineConstants.Contains('SECURE'))">
    <Compile Remove="SecureClass.cs" />
    <None Include="SecureClass.cs" />
  </ItemGroup>

也尝试过这种方法,但是它不起作用:

<!--#if (SECURE)-->
  <ItemGroup>
    <Compile Remove="SecureClass.cs" />
    <None Include="SecureClass.cs" />
  </ItemGroup>
<!--#endif-->

关于c# - 如何检查 csproj 中的条件编译符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61122780/

相关文章:

c# - 解析 XML 响应

c# - 使用 Rhino Mocks 的奇怪错误 "type doesn' t 匹配返回值”

如果程序集存在,则 C# 条件编译

c++ - C++ 中基于 ifdef 的继承问题

c# - 引用 .ccproj 中的运行时内容 (Azure SDK 2.9)

rust - 有没有一种方法可以基于常量的存在进行条件编译?

c# - NuGet 模块中的 MVC View

c# - Papercut 中的附件

msbuild-task - 如何在 csproj 文件中使用 Msbuild.Community.Tasks.Version

asp.net-core - Microsoft.AspNetCore.All元程序包的优缺点