I have a program and a library. the program has to use windows-dependency (WinForms or WPF ...) but the library should be platform-agnostic. I also want to be .Net-Version agnostic. So my library .csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net481;net9.0</TargetFrameworks>
</PropertyGroup>
</Project>
And the program .csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net481-windows;net9.0-windows</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TestProjLib\TestProjLib.csproj" />
</ItemGroup>
</Project>
The full solution with this test-setup can be found in my GitHub-Repository:
https://github.com/joecare99/CSharp/tree/master/TestStatements
For .Net9.0 this system works and the program compiles just fine, but for .Net481 it fails with CS0009: Metafile : [...] not found.
When copying the files to the needed place I get:
NETSDK1005: The Ressourcefile "[...]\TestProjLib\obj\project.assets.json" has no target for "net481-windows" [...]
I already tried the following solutions:
Is it a catch22 or is there a good stable solution ?
As Klaus Gütter pointed out, net481-windows
is not a valid MSBuild-target—even though it might work and the compiler seems to work normally?—it’s better to use net481
as the target. This applies to anyone who prefers the "new" .csproj
project file type.
Side note: I was surprised to find out that I had to change the "Main" project and not the library project.
Solution program .csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net481;net9.0-windows</TargetFrameworks>
<!-- ^ here net481 instead of net481-windows -->
<UseWindowsForms>true</UseWindowsForms>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TestProjLib\TestProjLib.csproj" />
</ItemGroup>
</Project>
This seems to work, after doing a lot of tests. More tests to do, but for now I am very thankful.
BTW: The example mentioned in the question has been reworked, and is working as expected.