Search code examples
c#visual-studio.net-6.0.net-standard-2.0multitargeting

How does Visual Studio picks up the current framework?


I started with a net6.0 project. And decided to provide netstandard2.0 as internal nuget output for the older supported builds.

Replaced <TargetFramework>net6.0</TargetFramework> with <TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks> in the .proj file. Now have this

enter image description here

The problem though is usage of the nullable reference types. net6.0 and netstandard2.0 are using different language versions

enter image description here

I understand that I would have to do one of these

  • Downgrade the language for net6.0
  • Create separate projects with certain common files and other separate files
  • Use conditional compilation

And I tried the conditional compilation

enter image description here

And it shows that current active framework is netstandard2.0. I was wondering, how to change that? I added symbols in "Conditional compilation symbols" on the project level, for "Debug and net6.0 Custom symbols". And I was able to switch that way. Now, I set the symbols so that netstandard2.0 is active. I rebuilt and what I see - it compiled for bin\Debug\net6.0 but there is nothing in bin\Debug\netstandard2.0. And also I get error -

Error CS8630 Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version '8.0' or greater. projectName (net6.0) C:\DEV\Components\projectName\CSC 1 Active

Not a single file has error inside. All references/packages seem resolving fine. I removed all net6.0-specific pieces in code files. Need some advise here. Thanks


Solution

  • In multi-target projects VS allows switching between the targets for intellisense and syntax highlighting - there is dropdown on the top left of the text editor (I use .NET 7 since I don't have .NET 6 installed):

    enter image description here

    For the JetBrains Rider users the same is possible by selecting the framework at the bottom of text editor:

    enter image description here

    Also in my reproducer I have added the conditional attribute to the Nullable element in the .csproj:

    <PropertyGroup Condition="'$(TargetFramework)' == 'net7.0'">
        <Nullable>enable</Nullable>
    </PropertyGroup>
    

    For only nullable you can do it also inline:

    <Nullable Condition="'$(TargetFramework)' == 'net7.0'">enable</Nullable>