Search code examples
c#csprojconditional-compilation

How to check for conditional compilation symbols in csproj


For security reasons, I need to exclude some code and some files from resulting exe. I'm using conditional compilation symbol SECURE for that. Excluding code is simple

#if !SECURE
// Some secure code
#endif

But for files - not so much. What should I write in Condition below to exclude these files only if SECURE symbol is defined?

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

Solution

  • After some digging, I found this github issue.

    So the answer is:

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

    Also tried this approach, but it didn't work:

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