Skip to content

Commit db89b79

Browse files
committed
Look for TargetFrameworkAttribute for CoreRuntime.
1 parent b7780b4 commit db89b79

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/BenchmarkDotNet/Environments/Runtimes/CoreRuntime.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public static CoreRuntime CreateForNewVersion(string msBuildMoniker, string disp
4646
return new CoreRuntime(RuntimeMoniker.NotRecognized, msBuildMoniker, displayName);
4747
}
4848

49+
internal static CoreRuntime GetTargetOrCurrentVersion(Assembly? assembly)
50+
// Try to determine the Framework version that the assembly was compiled for.
51+
=> GetTargetFrameworkVersion(assembly)
52+
// Fallback to the current running Framework version.
53+
?? GetCurrentVersion();
54+
4955
internal static CoreRuntime GetCurrentVersion()
5056
{
5157
if (!RuntimeInformation.IsNetCore)
@@ -243,5 +249,32 @@ private static CoreRuntime GetPlatformSpecific(CoreRuntime fallback)
243249

244250
return new CoreRuntime(fallback.RuntimeMoniker, $"{fallback.MsBuildMoniker}-{platformName}", fallback.Name);
245251
}
252+
253+
private static CoreRuntime? GetTargetFrameworkVersion(Assembly? assembly)
254+
{
255+
if (assembly is null)
256+
{
257+
return null;
258+
}
259+
260+
// Look for a TargetFrameworkAttribute with a supported Framework version.
261+
foreach (var attribute in assembly.GetCustomAttributes<TargetFrameworkAttribute>())
262+
{
263+
//.NETCoreApp,Version=vX.Y
264+
const string FrameworkPrefix = ".NETCoreApp,Version=v";
265+
var framework = attribute.FrameworkName;
266+
if (framework?.StartsWith(FrameworkPrefix) == true
267+
&& Version.TryParse(framework[FrameworkPrefix.Length..], out var version)
268+
// We don't support netcoreapp1.X
269+
&& version.Major >= 2)
270+
{
271+
return FromVersion(version);
272+
}
273+
}
274+
275+
// TargetFrameworkAttribute not found, or the assembly targeted a version older than we support,
276+
// or the assembly targeted a non-core framework (like netstandard2.0).
277+
return null;
278+
}
246279
}
247280
}

src/BenchmarkDotNet/Portability/RuntimeInformation.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,18 @@ string GetDetailedVersion()
171171
}
172172

173173
internal static Runtime GetTargetOrCurrentRuntime(Assembly? assembly)
174-
=> !IsMono && !IsWasm && IsFullFramework // Match order of checks in GetCurrentRuntime().
175-
? ClrRuntime.GetTargetOrCurrentVersion(assembly)
176-
: GetCurrentRuntime();
174+
{
175+
// Match order of checks in GetCurrentRuntime().
176+
if (!IsMono && !IsWasm)
177+
{
178+
if (IsFullFramework)
179+
return ClrRuntime.GetTargetOrCurrentVersion(assembly);
180+
// 99% of the time the core runtime is the same as the target framework, but the runtime could roll forward if it's not self-contained.
181+
if (IsNetCore)
182+
return CoreRuntime.GetTargetOrCurrentVersion(assembly);
183+
}
184+
return GetCurrentRuntime();
185+
}
177186

178187
internal static Runtime GetCurrentRuntime()
179188
{

0 commit comments

Comments
 (0)