How to convert between TFM (Target framework moniker) and FrameworkName?

1.4k views Asked by At

E.g.

netstandard2.0 (from Supported target frameworks) which is used as <TargetFramework> in *.csproj files or as folder name in NuGet packages internal structure,

and

.NETStandard,Version=v2.0 which is accepted by System.Runtime.Versioning.FrameworkName class's constructor or can be a value of TargetFrameworkAttribute.FrameworkName.

How to convert those strings from one form to another? At least one (any) direction.

1

There are 1 answers

3
Gabor On BEST ANSWER

You can use the source code of NuGet.Frameworks:

Here is the method that converts TFM to FrameworkName: https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Frameworks/NuGetFrameworkFactory.cs#L575

(e.g. netstandard2.0 to .NETStandard,Version=v2.0)

UPDATE #1

The good news is that it is available as a NuGet package: https://www.nuget.org/packages/NuGet.Frameworks/

Here is a .NET 6 Console Application:

using NuGet.Frameworks;
using System.Runtime.Versioning;

var tfmNetstandard20 = NuGetFramework.ParseFolder("netstandard20");

var fwNetstandard20 = new FrameworkName(tfmNetstandard20.DotNetFrameworkName);

Console.WriteLine(tfmNetstandard20);
Console.WriteLine(fwNetstandard20);

The output will be:

.NETStandard,Version=v2.0
.NETStandard,Version=v2.0