Nie da się tak zrobić, zresztą masz to w dokumentacji, którą masz wyżej – framework musi być zdefiniowany w pliku .csproj.
E:\_t\test>dotnet build -f net8.0
MSBuild version 17.8.3+195e7f5a3 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\8.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(266,5)
: error NETSDK1005: Assets file 'E:\_t\test\obj\project.assets.json' doesn't have a target for 'net8.0'. Ensure that re
store has run and that you have included 'net8.0' in the TargetFrameworks for your project. [E:\_t\test\test.csproj]
Build FAILED.
Jeżeli chcesz multi-targeting, to musisz zrobić tak, aby w .csproj był element <TargetFrameworks>
(zauważ "s" na końcu):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
I wtedy spoko, działa zarówno dotnet build -f net6.0
, jak i dotnet build -f net8.0
(ale net7.0
już nie).