Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between target frameworks for .NET Core projects in Visual Studio

Say you have a .NET Core project that looks like this:

"frameworks": {
    "net40": {},
    "dotnet5.1": {}
}

And this is your C# code:

public class Foo
{
    public static void Blah()
    {
#if DOTNET5_1
        DoSomething();
#elif NET40
        DoSomethingElse();
#endif
    }
}

Now, in Visual Studio when you view the .cs file, one of the #if sections will be grayed out- either DoSomething or DoSomethingElse. Here's how it shows up on my laptop:

enter image description here

Is it possible to get VS to 'switch context' between target platforms, so you can view what would be compiled for a particular platform? For example, I might want to check for any red squiggly lines for each framework before actually building the solution.

Any help would be appreciated, thanks!

like image 552
James Ko Avatar asked Mar 20 '16 19:03

James Ko


People also ask

How do I target multiple net frameworks?

To target multiple frameworks, change <TargetFramework> to plural <TargetFrameworks> and include monikers for different frameworks you want to target separated by ; . Here, we will support two more frameworks . NET Framework 4.0 & 4.6. So include net40 and net46 monikers respectively as shown below.

How do I change my target framework to .NET 5?

Open the solution with Visual Studio, right-click on the project, click on “Properties”, and on “Target Framework” change from “. NET 3.1” to “. NET 5”: Do the same for the other project's layers, including the unit tests projects.


2 Answers

At the top of your editor should be the navigation bar. Left in the navigation bar is a dropdown menu that lets you select the context.

If the navigation bar is hidden, you can enable it by going into Tools > Options > Text Editor > C# and check the navigation bar checkbox.

like image 128
mstaessen Avatar answered Sep 29 '22 19:09

mstaessen


Answer already provided seems mostly correct. I'd just like to highlight some issues currently present with that.

As of today, on VS 2015 Community Ed, Version 14.0.25424.00 Update 3, global.json sdk 1.0.0-preview2-003121, the nav bar shows multiple targets only if the startup project is a multi-target "executable" one (e.g console app).

multi-target switcher visible

If startup is set on a multi-target class library, no target switch is listed (and the label next to the green arrow is different as well):

multi-target switcher not visible

Also, when switcher can be used, it seems like "standard" conditional defines provided by build system are not correctly highlighted in editor:

enter image description here

In order to see conditional define blocks correctly highlighted, one has to switch the project switcher, highlighted in green in last screenshot. It seems like different targets in same project are treated as different projects to this purpose. And that can be changed indenpendently for any file open in editor.

like image 22
superjos Avatar answered Sep 29 '22 20:09

superjos