Targeting Multiple Frameworks for C# Projects

Ben Watts
3 min readMay 11, 2020
Multiple Paths

Some times we are stuck in a situation where we want to use our old .Net Framework library in our new .Net Core/Standard library. This can be achieved in many different ways, but lets go about this by allowing our library to be built for multiple frameworks.

We first create ourselves a new .Net Core console application (you could choose a .Net Core/Standard library, but you will need to reference this project to test it out). Lets go and add a new class called MultiFrameworks and place this code inside.

public string Platform
{
get
{
#if NET472
return ".NET Framework";
#elif NETSTANDARD2_0
return ".NET Standard";
#else
return ".Net Core";
#endif
}
}

Thanks to C#’s preprocessor we can check which framework we are currently building on.

Now lets use our new MultiFrameworks class in our Program.cs like so

static void Main(string[] args)
{
var library = new MultiFrameworks();
Console.WriteLine($"Platform: {library.Platform}");
Console.WriteLine($"Version: {Environment.Version}");
Console.WriteLine("Press return to exit...");
Console.ReadLine();
}

Now if you run the program you should see something like this.

I’m current building against .Net Core 3.1, however lets change our csproj so we can target multiple frameworks. Double click your csproj file to edit it.

You should now see something like this

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

To target multiple frameworks we can update our csproj to look like this

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netstandard2.0;net472;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
</Project>

We have updated the TargetFramework to TargetFrameworks and specified our frameworks. Now if you look at your Dependencies you should see three parents .NETCoreApp 3.1, .NETFramework 4.7.2 and .NETStandard 2.0.

Also if you build your project your bin/debug directory will look like this

As you can see our project is now outputting three different builds, so this project can now be used for those specific frameworks.

Now to actually test our MultiFramework class displays the correct output, we can change which build we want to run in Visual Studio 2019 like so.

If we now change it to net472, press play, we will see something like this in the console app.

Thank you for reading, I hope this has helped you or has taught you something new. Feel free to provide feedback, positive or negative.

References

Repository: https://github.com/MrApproved/Medium_MultiFramework
Image: https://www.pexels.com/photo/railroad-tracks-in-city-258510/
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/
https://docs.microsoft.com/en-us/dotnet/standard/frameworks
https://docs.microsoft.com/en-us/nuget/create-packages/multiple-target-frameworks-project-file

--

--