I have a simple netstandard2.1
library that references a Nuget and has a partial class that Nuget is referenced. I want to essentially have the class compile down to 2 DLLs, one with the Nuget and partial class included, and one without.
I've made a simple example here to showcase this: https://github.com/aherrick/ConditionalCompilation
What MSBuild sorcery do I need to achieve this?
CodePudding user response:
Excluding the file containing the partial class is not enough because as I see in your example the Location
property is referenced in other necessary files.
It may not be the cleanest solution but I had the same need and resolved using the preprocessor:
#ifndef EXCLUDE_VENUE_LOCATION
public partial class Venue
{
[System.Text.Json.Serialization.JsonIgnore]
public Geometry Location { get; set; }
}
#endif
And the same in Program.cs
#ifndef EXCLUDE_VENUE_LOCATION
venue.Location = null;
#endif
Then to conditionally set globally the define with msbuild you can compile as:
msbuild /p:DefineConstants=EXCLUDE_VENUE_LOCATION solution.sln
CodePudding user response:
Add:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <DefineConstants>$(DefineConstants)EXCLUDE_VENUE_LOCATION</DefineConstants> </PropertyGroup>
to your project file. Condition attribute is optional/as needed. It is not sorcery, but another way MS has chosen to input params to the command line of msbuild.
You can define or name a build configuration that lets you know that the nuget package is included or not.
BTW, msbuild runs a number of other pre-build files to verify everything in your project is kosher, and provide upstream project management. These are "*.targets" files.