Skip to content

Commit 8b2214b

Browse files
authored
use Environment.Version to determine .NET 5+ versions (#1580)
1 parent 178b6ad commit 8b2214b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ internal static bool TryGetVersion(out Version version)
7676
// we can't just use System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription
7777
// because it can be null and it reports versions like 4.6.* for .NET Core 2.*
7878

79+
// for .NET 5+ we can use Environment.Version
80+
if (Environment.Version.Major >= 5)
81+
{
82+
version = Environment.Version;
83+
return true;
84+
}
85+
7986
string runtimeDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
8087
if (TryGetVersionFromRuntimeDirectory(runtimeDirectory, out version))
8188
{
@@ -89,6 +96,9 @@ internal static bool TryGetVersion(out Version version)
8996
return true;
9097
}
9198

99+
// it's OK to use this method only after checking the previous ones
100+
// because we might have a benchmark app build for .NET Core X but executed using CoreRun Y
101+
// example: -f netcoreapp3.1 --corerun $omittedForBrevity\Microsoft.NETCore.App\6.0.0\CoreRun.exe - built as 3.1, run as 6.0 (#1576)
92102
string frameworkName = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
93103
if (TryGetVersionFromFrameworkName(frameworkName, out version))
94104
{

0 commit comments

Comments
 (0)