2020using System . Collections . Generic ;
2121using System . Linq ;
2222using System . Text . RegularExpressions ;
23- using ICSharpCode . Decompiler . TypeSystem . Implementation ;
2423using System . Reflection . Metadata ;
2524using System . Reflection . PortableExecutable ;
25+ using ICSharpCode . Decompiler . TypeSystem ;
2626
2727namespace ICSharpCode . Decompiler . Metadata
2828{
2929 public static class DotNetCorePathFinderExtensions
3030 {
31- static readonly string RefPathPattern =
31+ static readonly string PathPattern =
3232 @"(Reference Assemblies[/\\]Microsoft[/\\]Framework[/\\](?<type>.NETFramework)[/\\]v(?<version>[^/\\]+)[/\\])" +
3333 @"|((?<type>Microsoft\.NET)[/\\]assembly[/\\]GAC_(MSIL|32|64)[/\\])" +
3434 @"|((?<type>Microsoft\.NET)[/\\]Framework(64)?[/\\](?<version>[^/\\]+)[/\\])" +
3535 @"|(NuGetFallbackFolder[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)([/\\].*)?[/\\]ref[/\\])" +
36- @"|(shared[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)([/\\].*)?[/\\])" ;
36+ @"|(shared[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)([/\\].*)?[/\\])" +
37+ @"|(packs[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)\\ref([/\\].*)?[/\\])" ;
38+
39+ static readonly string RefPathPattern =
40+ @"(Reference Assemblies[/\\]Microsoft[/\\]Framework[/\\](?<type>.NETFramework)[/\\]v(?<version>[^/\\]+)[/\\])" +
41+ @"|(NuGetFallbackFolder[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)([/\\].*)?[/\\]ref[/\\])" +
42+ @"|(packs[/\\](?<type>[^/\\]+)\\(?<version>[^/\\]+)\\ref([/\\].*)?[/\\])" ;
3743
3844 public static string DetectTargetFrameworkId ( this PEFile assembly )
3945 {
@@ -99,7 +105,7 @@ public static string DetectTargetFrameworkId(this PEReader assembly, string asse
99105 * - .NETCore -> C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.1.0\ref\netcoreapp2.1\System.Console.dll
100106 * - .NETStandard -> C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.3\build\netstandard2.0\ref\netstandard.dll
101107 */
102- var pathMatch = Regex . Match ( assemblyPath , RefPathPattern ,
108+ var pathMatch = Regex . Match ( assemblyPath , PathPattern ,
103109 RegexOptions . IgnoreCase | RegexOptions . Compiled | RegexOptions . ExplicitCapture ) ;
104110 if ( pathMatch . Success ) {
105111 var type = pathMatch . Groups [ "type" ] . Value ;
@@ -121,6 +127,25 @@ public static string DetectTargetFrameworkId(this PEReader assembly, string asse
121127
122128 return string . Empty ;
123129 }
130+
131+ public static bool IsReferenceAssembly ( this PEFile assembly )
132+ {
133+ return IsReferenceAssembly ( assembly . Reader , assembly . FileName ) ;
134+ }
135+
136+ public static bool IsReferenceAssembly ( this PEReader assembly , string assemblyPath )
137+ {
138+ if ( assembly == null )
139+ throw new ArgumentNullException ( nameof ( assembly ) ) ;
140+
141+ var metadata = assembly . GetMetadataReader ( ) ;
142+ if ( metadata . GetCustomAttributes ( Handle . AssemblyDefinition ) . HasKnownAttribute ( metadata , KnownAttribute . ReferenceAssembly ) )
143+ return true ;
144+
145+ // Try to detect reference assembly through specific path pattern
146+ var refPathMatch = Regex . Match ( assemblyPath , RefPathPattern , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
147+ return refPathMatch . Success ;
148+ }
124149 }
125150
126151 public class ReferenceLoadInfo
0 commit comments