diff --git a/src/Docfx.Dotnet/CompilationHelper.cs b/src/Docfx.Dotnet/CompilationHelper.cs index 69d3463828f..3477aca4f4c 100644 --- a/src/Docfx.Dotnet/CompilationHelper.cs +++ b/src/Docfx.Dotnet/CompilationHelper.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. - +using System.Reflection.PortableExecutable; using Docfx.Common; using Docfx.Exceptions; using ICSharpCode.Decompiler.Metadata; @@ -103,7 +103,7 @@ public static Compilation CreateCompilationFromVBCode(string code, IDictionary GetDefaultMetadataReferences(strin } } - private static IEnumerable GetReferenceAssemblies(string assemblyPath, MetadataReference[] references) + private static IEnumerable GetReferenceAssemblies(string assemblyPath, PEReader? peReader, MetadataReference[] references) { - using var assembly = new PEFile(assemblyPath); + using var assembly = peReader == null + ? new PEFile(assemblyPath) + : new PEFile(assemblyPath, peReader); var assemblyResolver = new UniversalAssemblyResolver(assemblyPath, false, assembly.DetectTargetFrameworkId()); var result = new Dictionary(); Dictionary? referenceFiles = default; diff --git a/src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs b/src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs index 60e8b4be1e3..4177b3d9dbe 100644 --- a/src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs +++ b/src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Reflection.PortableExecutable; using Docfx.Common; using Microsoft.Build.Construction; using Microsoft.Build.Framework; @@ -109,8 +110,17 @@ await LoadCompilationFromProject(project.AbsolutePath) is { } compilation) { foreach (var assemblyFile in assemblyFiles) { - Logger.LogInfo($"Loading assembly {assemblyFile.NormalizedPath}"); - var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(assemblyFile.NormalizedPath, config.IncludePrivateMembers, metadataReferences); + var normalizedAssemblyPath = assemblyFile.NormalizedPath; + + using var peReader = new PEReader(new FileStream(normalizedAssemblyPath, FileMode.Open, FileAccess.Read)); + if (!peReader.HasMetadata) + { + Logger.LogInfo($"Skip non-managed assembly {normalizedAssemblyPath}"); + continue; + } + + Logger.LogInfo($"Loading assembly {normalizedAssemblyPath}"); + var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(normalizedAssemblyPath, peReader, config.IncludePrivateMembers, metadataReferences); hasCompilationError |= compilation.CheckDiagnostics(config.AllowCompilationErrors); assemblies.Add((assembly, compilation)); }