diff --git a/src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs b/src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs index 95ff5efb83..6cb6cd0f34 100644 --- a/src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs +++ b/src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs @@ -76,6 +76,13 @@ internal static bool TryGetVersion(out Version version) // we can't just use System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription // because it can be null and it reports versions like 4.6.* for .NET Core 2.* + // for .NET 5+ we can use Environment.Version + if (Environment.Version.Major >= 5) + { + version = Environment.Version; + return true; + } + string runtimeDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); if (TryGetVersionFromRuntimeDirectory(runtimeDirectory, out version)) { @@ -89,6 +96,9 @@ internal static bool TryGetVersion(out Version version) return true; } + // it's OK to use this method only after checking the previous ones + // because we might have a benchmark app build for .NET Core X but executed using CoreRun Y + // example: -f netcoreapp3.1 --corerun $omittedForBrevity\Microsoft.NETCore.App\6.0.0\CoreRun.exe - built as 3.1, run as 6.0 (#1576) string frameworkName = Assembly.GetEntryAssembly()?.GetCustomAttribute()?.FrameworkName; if (TryGetVersionFromFrameworkName(frameworkName, out version)) {