Can a NuGet package define a preprocessor symbol for the consuming project? And if so, how?
If your package (My.ExamplePackage
) contains an MSBuild file named like the package name + .targets
in the build directory (so build\MyExamplePackage.targets
), you can use it to amend the DefineConstants
property that may have been set to defaults or reset in the project:
<Project>
<PropertyGroup>
<DefineConstants>$(DefineConstants);MY_PREPOCESSOR_SYMBOL</DefineConstants>
</PropertyGroup>
</Project>
While the package can contain both a .props
and .targets
file, the .targets
file will be imported after the main body of the csproj file being built so if it the project re-sets DefineConstants
(meaning not adding to it by prepending $(DefineConstant);
), the .targtes
file has a chance of adding to this property.