Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit bf2f35f

Browse files
saferndanmoseley
authored andcommitted
Fix PlatformDetection.GetFrameworkVersion()
1 parent 81d852c commit bf2f35f

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/Common/tests/System/PlatformDetection.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,18 @@ public static Version GetFrameworkVersion()
8282
{
8383
string[] descriptionArray = RuntimeInformation.FrameworkDescription.Split(' ');
8484
if (descriptionArray.Length < 3)
85-
{
8685
return null;
87-
}
88-
89-
string runningVersion = descriptionArray[2];
9086

91-
// we could get a version with build number > 1 e.g 4.6.1375 but we only want to have 4.6.1
92-
// so that we get the actual Framework Version
93-
if (runningVersion.Length > 5)
87+
if (!Version.TryParse(descriptionArray[2], out Version actualVersion))
88+
return null;
89+
90+
foreach (Range currentRange in FrameworkRanges)
9491
{
95-
runningVersion = runningVersion.Substring(0, 5);
92+
if (currentRange.IsInRange(actualVersion))
93+
return currentRange.FrameworkVersion;
9694
}
9795

98-
Version result;
99-
return Version.TryParse(runningVersion, out result) ? result : null;
96+
return null;
10097
}
10198

10299
private static int s_isWinRT = -1;
@@ -405,5 +402,33 @@ public static bool IsNonZeroLowerBoundArraySupported
405402
// System.Security.Cryptography.Xml.XmlDsigXsltTransform.GetOutput() relies on XslCompiledTransform which relies
406403
// heavily on Reflection.Emit
407404
public static bool IsXmlDsigXsltTransformSupported => PlatformDetection.IsReflectionEmitSupported;
405+
406+
public static Range[] FrameworkRanges => new Range[]{
407+
new Range(new Version(4, 7, 2500, 0), null, new Version(4, 7, 1)),
408+
new Range(new Version(4, 6, 2000, 0), new Version(4, 7, 2090, 0), new Version(4, 7, 0)),
409+
new Range(new Version(4, 6, 1500, 0), new Version(4, 6, 1999, 0), new Version(4, 6, 2)),
410+
new Range(new Version(4, 6, 1000, 0), new Version(4, 6, 1499, 0), new Version(4, 6, 1)),
411+
new Range(new Version(4, 6, 55, 0), new Version(4, 6, 999, 0), new Version(4, 6, 0)),
412+
new Range(new Version(4, 0, 30319, 0), new Version(4, 0, 52313, 36313), new Version(4, 5, 2))
413+
};
414+
415+
public class Range
416+
{
417+
public Version Start { get; private set; }
418+
public Version Finish { get; private set; }
419+
public Version FrameworkVersion { get; private set; }
420+
421+
public Range(Version start, Version finish, Version frameworkVersion)
422+
{
423+
Start = start;
424+
Finish = finish;
425+
FrameworkVersion = frameworkVersion;
426+
}
427+
428+
public bool IsInRange(Version version)
429+
{
430+
return version >= Start && (Finish == null || version <= Finish);
431+
}
432+
}
408433
}
409434
}

0 commit comments

Comments
 (0)