Reputation: 3118
I have a project that is currently compiled for .Net 4.0 and .Net 4.5. I also have a different project that uses .Net Core 2.0.
There is code that is shared between them, it would be very helpful if I could create a .Net Standard 2.0 class library, but we still need to support .Net Framework 4.0.
Is there any way to compile the class library for both .Net Framework 4.0 and as a .Net Standard 2.0 class library (Assuming the class library doesn't use any dependencies that aren't supported by both)?
Upvotes: 2
Views: 252
Reputation: 1063318
In short:
<TargetFrameworks>net40;netstandard2.0</TargetFrameworks>
When you build, this will create each target separately. If you are using automatic nupkg generation (<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
), the nupkg will contain all targets. If you need to configure things differently for each, you can test the TargetFramework
variable in the build file, which will be applied individually per target. This can be used to add different references or other configuration options, for example:
<PropertyGroup Condition="'$(TargetFramework)' == 'net20'">
<FeatureServiceModel>false</FeatureServiceModel>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<DefineConstants>$(DefineConstants);COREFX</DefineConstants>
<ImportLibs>core</ImportLibs>
<FeatureServiceModel>false</FeatureServiceModel>
</PropertyGroup>
Upvotes: 1