From 62cdf38e9b96f87add9adb9281fdee439e74b6ee Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 13 Dec 2024 02:18:44 +0100 Subject: [PATCH] Fix #3349: Get rid of version string manipulation in UniversalAssemblyResolver.ParseTargetFramework. --- .../ProjectDecompiler/TargetFrameworkTests.cs | 10 ++++++++++ .../Metadata/UniversalAssemblyResolver.cs | 15 ++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/ProjectDecompiler/TargetFrameworkTests.cs b/ICSharpCode.Decompiler.Tests/ProjectDecompiler/TargetFrameworkTests.cs index df78e9bc1b..1a9557c77f 100644 --- a/ICSharpCode.Decompiler.Tests/ProjectDecompiler/TargetFrameworkTests.cs +++ b/ICSharpCode.Decompiler.Tests/ProjectDecompiler/TargetFrameworkTests.cs @@ -19,6 +19,7 @@ using System; using ICSharpCode.Decompiler.CSharp.ProjectDecompiler; +using ICSharpCode.Decompiler.Metadata; using NUnit.Framework; @@ -119,5 +120,14 @@ public void VerifyMoniker(string identifier, int version, string expectedMoniker // Assert Assert.That(targetFramework.Moniker, Is.EqualTo(expectedMoniker)); } + + [TestCase(".NETCoreApp, Version=v5.0", TargetFrameworkIdentifier.NET, "5.0.0")] + [TestCase(".NETCoreApp, Version=v10.0", TargetFrameworkIdentifier.NET, "10.0.0")] + public void VerifyUniversalAssemblyResolverParseTargetFramework(string targetFramework, TargetFrameworkIdentifier identifier, string version) + { + var (id, v) = UniversalAssemblyResolver.ParseTargetFramework(targetFramework); + Assert.That(id, Is.EqualTo(identifier)); + Assert.That(v.ToString(3), Is.EqualTo(version)); + } } } diff --git a/ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs b/ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs index 90ba8724ac..ffd6d700d2 100644 --- a/ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs +++ b/ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs @@ -189,15 +189,16 @@ internal static (TargetFrameworkIdentifier, Version) ParseTargetFramework(string switch (pair[0].Trim().ToUpperInvariant()) { case "VERSION": - var versionString = pair[1].TrimStart('v', ' ', '\t'); - if (identifier == TargetFrameworkIdentifier.NETCoreApp || - identifier == TargetFrameworkIdentifier.NETStandard) - { - if (versionString.Length == 3) - versionString += ".0"; - } + var versionString = pair[1].TrimStart('v', 'V', ' ', '\t'); + if (!Version.TryParse(versionString, out version)) + { version = null; + } + else + { + version = new Version(version.Major, version.Minor, version.Build < 0 ? 0 : version.Build); + } // .NET 5 or greater still use ".NETCOREAPP" as TargetFrameworkAttribute value... if (version?.Major >= 5 && identifier == TargetFrameworkIdentifier.NETCoreApp) identifier = TargetFrameworkIdentifier.NET;