11using System ;
22using System . IO ;
33using System . Linq ;
4+ using System . Reflection ;
5+ using System . Runtime . Versioning ;
46using Microsoft . Win32 ;
57
68namespace BenchmarkDotNet . Helpers
@@ -10,15 +12,41 @@ internal static class FrameworkVersionHelper
1012 // magic numbers come from https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
1113 // should be ordered by release number
1214 private static readonly ( int minReleaseNumber , string version ) [ ] FrameworkVersions =
13- {
15+ [
1416 ( 533320 , "4.8.1" ) , // value taken from Windows 11 arm64 insider build
1517 ( 528040 , "4.8" ) ,
1618 ( 461808 , "4.7.2" ) ,
1719 ( 461308 , "4.7.1" ) ,
1820 ( 460798 , "4.7" ) ,
1921 ( 394802 , "4.6.2" ) ,
2022 ( 394254 , "4.6.1" )
21- } ;
23+ ] ;
24+
25+ internal static string ? GetTargetFrameworkVersion ( Assembly ? assembly )
26+ {
27+ if ( assembly is null )
28+ {
29+ return null ;
30+ }
31+
32+ // Look for a TargetFrameworkAttribute with a supported Framework version.
33+ foreach ( var attribute in assembly . GetCustomAttributes < TargetFrameworkAttribute > ( ) )
34+ {
35+ switch ( attribute . FrameworkName )
36+ {
37+ case ".NETFramework,Version=v4.6.1" : return "4.6.1" ;
38+ case ".NETFramework,Version=v4.6.2" : return "4.6.2" ;
39+ case ".NETFramework,Version=v4.7" : return "4.7" ;
40+ case ".NETFramework,Version=v4.7.1" : return "4.7.1" ;
41+ case ".NETFramework,Version=v4.7.2" : return "4.7.2" ;
42+ case ".NETFramework,Version=v4.8" : return "4.8" ;
43+ case ".NETFramework,Version=v4.8.1" : return "4.8.1" ;
44+ }
45+ }
46+
47+ // TargetFrameworkAttribute not found, or the assembly targeted a version older than we support.
48+ return null ;
49+ }
2250
2351 internal static string GetFrameworkDescription ( )
2452 {
@@ -57,30 +85,28 @@ internal static string MapToReleaseVersion(string servicingVersion)
5785
5886
5987#if NET6_0_OR_GREATER
60- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
88+ [ SupportedOSPlatform ( "windows" ) ]
6189#endif
6290 private static int ? GetReleaseNumberFromWindowsRegistry ( )
6391 {
64- using ( var baseKey = RegistryKey . OpenBaseKey ( RegistryHive . LocalMachine , RegistryView . Registry32 ) )
65- using ( var ndpKey = baseKey . OpenSubKey ( @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" ) )
66- {
67- if ( ndpKey == null )
68- return null ;
69- return Convert . ToInt32 ( ndpKey . GetValue ( "Release" ) ) ;
70- }
92+ using var baseKey = RegistryKey . OpenBaseKey ( RegistryHive . LocalMachine , RegistryView . Registry32 ) ;
93+ using var ndpKey = baseKey . OpenSubKey ( @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" ) ;
94+ if ( ndpKey == null )
95+ return null ;
96+ return Convert . ToInt32 ( ndpKey . GetValue ( "Release" ) ) ;
7197 }
7298
7399#if NET6_0_OR_GREATER
74- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
100+ [ SupportedOSPlatform ( "windows" ) ]
75101#endif
76- internal static string GetLatestNetDeveloperPackVersion ( )
102+ internal static string ? GetLatestNetDeveloperPackVersion ( )
77103 {
78- if ( ! ( GetReleaseNumberFromWindowsRegistry ( ) is int releaseNumber ) )
104+ if ( GetReleaseNumberFromWindowsRegistry ( ) is not int releaseNumber )
79105 return null ;
80106
81107 return FrameworkVersions
82- . FirstOrDefault ( v => releaseNumber >= v . minReleaseNumber && IsDeveloperPackInstalled ( v . version ) )
83- . version ;
108+ . FirstOrDefault ( v => releaseNumber >= v . minReleaseNumber && IsDeveloperPackInstalled ( v . version ) )
109+ . version ;
84110 }
85111
86112 // Reference Assemblies exists when Developer Pack is installed
0 commit comments