@@ -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}
0 commit comments