Search code examples
c#visual-studio-2019projectsolutionpreprocessor-directive

Defining C# preprocessor directive at the Solution level


I have a question Based on This question

Is it possible in C# to define a preprocessor directive at the solution level rather than the project level.

Motivation: I have a common library, and there are two solutions that need it. I want the preprocessor directive to be present in one, and not the other, but since the library is at the project level, if I define the preprocessor directive at the project level, this obviously won't work.


Solution

  • You can do this with a Directory.Build.props file, and put it at the root of your solution. Inside the file you can specify DefineConstants option:

    <Project>
      <PropertyGroup>
        <DefineConstants>MY_DIRECTIVE</DefineConstants>
      </PropertyGroup>
    </Project>