11using System ;
2- using System . Collections . Generic ;
3- using System . Diagnostics ;
42using System . IO ;
53using System . Linq ;
64using System . Reflection ;
@@ -24,54 +22,25 @@ private static readonly (int minReleaseNumber, string version)[] FrameworkVersio
2422 ( 394254 , "4.6.1" )
2523 ] ;
2624
27- internal static string ? GetTargetFrameworkVersion ( )
25+ internal static string ? GetTargetFrameworkVersion ( Assembly ? assembly )
2826 {
29- // Search assemblies until we find a TargetFrameworkAttribute with a supported Framework version.
30- // We don't search all assemblies, only the entry assembly and callers.
31- foreach ( var assembly in EnumerateAssemblies ( ) )
27+ // Look for a TargetFrameworkAttribute with a supported Framework version.
28+ foreach ( var attribute in assembly . GetCustomAttributes < TargetFrameworkAttribute > ( ) )
3229 {
33- foreach ( var attribute in assembly . GetCustomAttributes < TargetFrameworkAttribute > ( ) )
30+ switch ( attribute . FrameworkName )
3431 {
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- }
32+ case ".NETFramework,Version=v4.6.1" : return "4.6.1" ;
33+ case ".NETFramework,Version=v4.6.2" : return "4.6.2" ;
34+ case ".NETFramework,Version=v4.7" : return "4.7" ;
35+ case ".NETFramework,Version=v4.7.1" : return "4.7.1" ;
36+ case ".NETFramework,Version=v4.7.2" : return "4.7.2" ;
37+ case ".NETFramework,Version=v4.8" : return "4.8" ;
38+ case ".NETFramework,Version=v4.8.1" : return "4.8.1" ;
4539 }
4640 }
4741
42+ // TargetFrameworkAttribute not found, or the assembly targeted a version older than we support.
4843 return null ;
49-
50- static IEnumerable < Assembly > EnumerateAssemblies ( )
51- {
52- var entryAssembly = Assembly . GetEntryAssembly ( ) ;
53- // Assembly.GetEntryAssembly() returns null in unit test frameworks.
54- if ( entryAssembly != null )
55- {
56- yield return entryAssembly ;
57- }
58- // Search calling assemblies starting from the highest stack frame
59- // (expected to be the entry assembly if Assembly.GetEntryAssembly() returned null),
60- // excluding this assembly.
61- var stacktrace = new StackTrace ( false ) ;
62- var searchedAssemblies = new HashSet < Assembly > ( )
63- {
64- stacktrace . GetFrame ( 0 ) . GetMethod ( ) . ReflectedType . Assembly
65- } ;
66- for ( int i = stacktrace . FrameCount - 1 ; i >= 1 ; -- i )
67- {
68- var assembly = stacktrace . GetFrame ( i ) . GetMethod ( ) . ReflectedType . Assembly ;
69- if ( searchedAssemblies . Add ( assembly ) )
70- {
71- yield return assembly ;
72- }
73- }
74- }
7544 }
7645
7746 internal static string GetFrameworkDescription ( )
0 commit comments