Skip to content

Commit

Permalink
Feat/private symbols (dotnet#9972)
Browse files Browse the repository at this point in the history
* (fix): when specifying "includePrivateMembers":"true" in the docfx.json file, enumerates all the private members of the classes.

Todo:
- remove class constructor;
- remove compiler generated members.

* Removes class constructors and compiler generated members.

* - Use a named parameter to specify the import options.
- Refactoring the parameter to directly pass a boolean value.
  • Loading branch information
Patrick8639 authored and p-kostov committed Jun 28, 2024
1 parent 026581a commit 7190dcb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Docfx.Dotnet/CompilationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +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 Docfx.Common;
using Docfx.Exceptions;
using ICSharpCode.Decompiler.Metadata;
Expand Down Expand Up @@ -102,12 +103,17 @@ public static Compilation CreateCompilationFromVBCode(string code, IDictionary<s
references: GetDefaultMetadataReferences("VB").Concat(references ?? []));
}

public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(string assemblyPath, params MetadataReference[] references)
public static (Compilation, IAssemblySymbol) CreateCompilationFromAssembly(string assemblyPath, bool includePrivateMembers = false, params MetadataReference[] references)
{
var metadataReference = CreateMetadataReference(assemblyPath);
var compilation = CS.CSharpCompilation.Create(
assemblyName: null,
options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
options: new CS.CSharpCompilationOptions(
outputKind : OutputKind.DynamicallyLinkedLibrary,
metadataImportOptions : includePrivateMembers
? MetadataImportOptions.All
: MetadataImportOptions.Public
),
syntaxTrees: s_assemblyBootstrap,
references: GetReferenceAssemblies(assemblyPath)
.Select(CreateMetadataReference)
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.Dotnet/DotnetApiCatalog.Compile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ await LoadCompilationFromProject(project.AbsolutePath) is { } compilation)
foreach (var assemblyFile in assemblyFiles)
{
Logger.LogInfo($"Loading assembly {assemblyFile.NormalizedPath}");
var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(assemblyFile.NormalizedPath, metadataReferences);
var (compilation, assembly) = CompilationHelper.CreateCompilationFromAssembly(assemblyFile.NormalizedPath, config.IncludePrivateMembers, metadataReferences);
hasCompilationError |= compilation.CheckDiagnostics(config.AllowCompilationErrors);
assemblies.Add((assembly, compilation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ public override MetadataItem VisitNamedType(INamedTypeSymbol symbol)
}

item.Items = new List<MetadataItem>();
foreach (var member in symbol.GetMembers().Where(s => s is not INamedTypeSymbol))
foreach (
var member in symbol.GetMembers()
.Where(static s =>
s is not INamedTypeSymbol
&& ! s.Name.StartsWith('<')
&& (s is not IMethodSymbol ms || ms.MethodKind != MethodKind.StaticConstructor)
))
{
var memberItem = member.Accept(this);
if (memberItem != null)
Expand Down

0 comments on commit 7190dcb

Please sign in to comment.