Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional diagnostic info #73341

Merged
merged 5 commits into from
May 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down Expand Up @@ -203,13 +204,15 @@ private static void WriteMvidTo(ModuleMetadata metadata, ObjectWriter writer, Ca
cancellationToken.ThrowIfCancellationRequested();

writer.WriteInt32((int)metadata.Kind);
writer.WriteGuid(GetMetadataGuid(metadata));
}

private static Guid GetMetadataGuid(ModuleMetadata metadata)
{
var metadataReader = metadata.GetMetadataReader();

var mvidHandle = metadataReader.GetModuleDefinition().Mvid;
var guid = metadataReader.GetGuid(mvidHandle);

writer.WriteGuid(guid);
return guid;
}

private static void WritePortableExecutableReferenceTo(
Expand Down Expand Up @@ -535,5 +538,36 @@ protected override Metadata GetMetadataImpl()

protected override PortableExecutableReference WithPropertiesImpl(MetadataReferenceProperties properties)
=> new SerializedMetadataReference(properties, FilePath, _metadata, _storageHandles, _provider);

public override string ToString()
{
var metadata = TryGetMetadata(this);
var modules = GetModules(metadata);

return $"""
{nameof(SerializedMetadataReference)}
FilePath={this.FilePath}
Kind={this.Properties.Kind}
Aliases={this.Properties.Aliases.Join(",")}
EmbedInteropTypes={this.Properties.EmbedInteropTypes}
MetadataKind={metadata switch { null => "null", AssemblyMetadata => "assembly", ModuleMetadata => "module", _ => metadata.GetType().Name }}
Guids={modules.Select(m => GetMetadataGuid(m).ToString()).Join(",")}
""";

ImmutableArray<ModuleMetadata> GetModules(Metadata? metadata)
{
if (metadata is AssemblyMetadata assemblyMetadata)
{
if (TryGetModules(assemblyMetadata, out var modules))
return modules;
}
else if (metadata is ModuleMetadata moduleMetadata)
{
return [moduleMetadata];
}

return [];
}
}
}
}
Loading