So for my C# projects I really enjoy using the Directory.Build.props e.g. for settings the dotnet version for the entire solution.
However, there is one hiccup for this.
When I set my version in the Directory.Build.props to e.g. <TargetFramework>net8.0</TargetFramework>
this will not target dotnet 8 for Windows, which is required when working with frameworks like WPF, which would need something like this <TargetFramework>net8.0-windows</TargetFramework>
.
Now this all does make sense, but I would love it if it were possible to "reuse" the set framework on the props level for my WPF project, would this be possible?
So it could look something like this
Directory.Build.Props
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
...
</PropertyGroup>
...
</Project>
wpfProject.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)-windows</TargetFramework>
...
</PropertyGroup>
</Project>
Would this work or would there be a catch?
That works - mostly.
I've used this approach for quite some time, but I found that directly specifying <TargetFramework>
in Directory.Build.props sometimes results in unexpected behavior (e.g compiler errors about not having the target framework set for a particular project.) It seems that it's best to always explicitly include <TargetFramework>
(or <TargetFrameworks>
when multi-targetting) in the csproj files.
Therefore, my current approach is as follows. In Directory.Build.props
:
<Project>
<PropertyGroup>
<TargetFrameworkBaseVersion>net8.0</TargetFrameworkBaseVersion>
...
</PropertyGroup>
...
</Project>
and in the .csproj files:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFrameworkBaseVersion)-windows</TargetFramework><!-- With "-windows" when needed, without it otherwise -->
...
</PropertyGroup>
</Project>