From f93c478a6d2373a99d07ed7165c59506e0a34a2b Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sat, 16 Mar 2024 15:28:14 +0100 Subject: [PATCH] ExportedTypes, TypeReference and MemberReferences: Use UnknownType/FakeMember for pretty-printing of signatures in TreeView. --- ...ssemblyReferenceReferencedTypesTreeNode.cs | 7 ++++--- ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs | 8 +++++--- ILSpy/TreeNodes/ExportedTypeTreeNode.cs | 12 +++++++----- ILSpy/TreeNodes/MemberReferenceTreeNode.cs | 19 +++++++++++++------ ILSpy/TreeNodes/ReferenceFolderTreeNode.cs | 4 +++- ILSpy/TreeNodes/TypeReferenceTreeNode.cs | 13 ++++++++----- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs b/ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs index 6d4734a916..37511236f2 100644 --- a/ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs @@ -20,6 +20,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.ILSpy.TreeNodes { @@ -28,10 +29,10 @@ namespace ICSharpCode.ILSpy.TreeNodes /// public sealed class AssemblyReferenceReferencedTypesTreeNode : ILSpyTreeNode { - private readonly PEFile module; - private readonly AssemblyReference r; + readonly MetadataModule module; + readonly AssemblyReference r; - public AssemblyReferenceReferencedTypesTreeNode(PEFile module, AssemblyReference r) + public AssemblyReferenceReferencedTypesTreeNode(MetadataModule module, AssemblyReference r) { this.module = module ?? throw new ArgumentNullException(nameof(module)); this.r = r ?? throw new ArgumentNullException(nameof(r)); diff --git a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs index 8aa2501a37..3db5f974ee 100644 --- a/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs +++ b/ILSpy/TreeNodes/AssemblyReferenceTreeNode.cs @@ -21,6 +21,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.ILSpy.TreeNodes { @@ -29,11 +30,11 @@ namespace ICSharpCode.ILSpy.TreeNodes /// public sealed class AssemblyReferenceTreeNode : ILSpyTreeNode { - readonly PEFile module; + readonly MetadataModule module; readonly AssemblyReference r; readonly AssemblyTreeNode parentAssembly; - public AssemblyReferenceTreeNode(PEFile module, AssemblyReference r, AssemblyTreeNode parentAssembly) + public AssemblyReferenceTreeNode(MetadataModule module, AssemblyReference r, AssemblyTreeNode parentAssembly) { this.module = module ?? throw new ArgumentNullException(nameof(module)); this.r = r ?? throw new ArgumentNullException(nameof(r)); @@ -83,8 +84,9 @@ protected override void LoadChildren() var referencedModule = resolver.Resolve(r); if (referencedModule != null) { + var module = (MetadataModule)referencedModule.GetTypeSystemWithCurrentOptionsOrNull().MainModule; foreach (var childRef in referencedModule.AssemblyReferences) - this.Children.Add(new AssemblyReferenceTreeNode(referencedModule, childRef, parentAssembly)); + this.Children.Add(new AssemblyReferenceTreeNode(module, childRef, parentAssembly)); } } diff --git a/ILSpy/TreeNodes/ExportedTypeTreeNode.cs b/ILSpy/TreeNodes/ExportedTypeTreeNode.cs index bb21545c8f..b01e0c1dfc 100644 --- a/ILSpy/TreeNodes/ExportedTypeTreeNode.cs +++ b/ILSpy/TreeNodes/ExportedTypeTreeNode.cs @@ -29,19 +29,21 @@ namespace ICSharpCode.ILSpy.TreeNodes /// public sealed class ExportedTypeTreeNode : ILSpyTreeNode { - readonly PEFile module; - private readonly ExportedTypeMetadata r; + readonly MetadataModule module; + readonly ExportedTypeMetadata r; + readonly IType resolvedType; - public ExportedTypeTreeNode(PEFile module, ExportedTypeMetadata r) + public ExportedTypeTreeNode(MetadataModule module, ExportedTypeMetadata r) { this.module = module ?? throw new ArgumentNullException(nameof(module)); this.r = r ?? throw new ArgumentNullException(nameof(r)); + this.resolvedType = module.ResolveType(r.Handle, default); this.LazyLoading = true; } public override object Text - => Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false) + GetSuffixString(r.Handle); + => Language.TypeToString(resolvedType, includeNamespace: true) + GetSuffixString(r.Handle); public override object Icon => Images.Library; @@ -55,7 +57,7 @@ protected override void LoadChildren() public override void Decompile(Language language, ITextOutput output, DecompilationOptions options) { - language.WriteCommentLine(output, $"{Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false)} (Exported, IsForwarder: {r.IsForwarder}, Attributes: {r.Attributes})"); + language.WriteCommentLine(output, $"{Language.TypeToString(resolvedType, includeNamespace: true)} (Exported, IsForwarder: {r.IsForwarder}, Attributes: {(int)r.Attributes:X8})"); } } } diff --git a/ILSpy/TreeNodes/MemberReferenceTreeNode.cs b/ILSpy/TreeNodes/MemberReferenceTreeNode.cs index c694430295..a320d5ccb7 100644 --- a/ILSpy/TreeNodes/MemberReferenceTreeNode.cs +++ b/ILSpy/TreeNodes/MemberReferenceTreeNode.cs @@ -23,6 +23,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.ILSpy.TreeNodes { @@ -31,16 +32,22 @@ namespace ICSharpCode.ILSpy.TreeNodes /// public sealed class MemberReferenceTreeNode : ILSpyTreeNode { - readonly PEFile module; - private readonly MemberReferenceMetadata r; + readonly MetadataModule module; + readonly MemberReferenceMetadata r; + readonly IMember resolvedMember; - public MemberReferenceTreeNode(PEFile module, MemberReferenceMetadata r) + public MemberReferenceTreeNode(MetadataModule module, MemberReferenceMetadata r) { this.module = module ?? throw new ArgumentNullException(nameof(module)); this.r = r; + this.resolvedMember = r.MemberReferenceKind switch { + MemberReferenceKind.Method => module.ResolveMethod(r.Handle, default), + MemberReferenceKind.Field => (IMember)module.ResolveEntity(r.Handle), + _ => throw new NotSupportedException(), + }; } - public override object Text => r.Name + GetSuffixString(r.Handle); + public override object Text => Signature + GetSuffixString(r.Handle); public override object Icon => r.MemberReferenceKind switch { MemberReferenceKind.Method => Images.Method, @@ -48,13 +55,13 @@ public MemberReferenceTreeNode(PEFile module, MemberReferenceMetadata r) _ => Images.Class, }; - public string Signature => Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false); + public string Signature => resolvedMember is IMethod m ? Language.MethodToString(m, false, false, false) : Language.FieldToString((IField)resolvedMember, false, false, false); public override void Decompile(Language language, ITextOutput output, DecompilationOptions options) { language.WriteCommentLine(output, Signature); } - public override object ToolTip => Signature; + public override object ToolTip => Language.GetRichTextTooltip(resolvedMember); } } diff --git a/ILSpy/TreeNodes/ReferenceFolderTreeNode.cs b/ILSpy/TreeNodes/ReferenceFolderTreeNode.cs index 9d8452d4a5..e978a7e8a5 100644 --- a/ILSpy/TreeNodes/ReferenceFolderTreeNode.cs +++ b/ILSpy/TreeNodes/ReferenceFolderTreeNode.cs @@ -22,6 +22,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.ILSpy.Properties; namespace ICSharpCode.ILSpy.TreeNodes @@ -48,8 +49,9 @@ public ReferenceFolderTreeNode(PEFile module, AssemblyTreeNode parentAssembly) protected override void LoadChildren() { var metadata = module.Metadata; + var metadataModule = (MetadataModule)module.GetTypeSystemWithCurrentOptionsOrNull().MainModule; foreach (var r in module.AssemblyReferences.OrderBy(r => r.Name)) - this.Children.Add(new AssemblyReferenceTreeNode(module, r, parentAssembly)); + this.Children.Add(new AssemblyReferenceTreeNode(metadataModule, r, parentAssembly)); foreach (var r in metadata.GetModuleReferences().OrderBy(r => metadata.GetString(metadata.GetModuleReference(r).Name))) this.Children.Add(new ModuleReferenceTreeNode(parentAssembly, r, module)); } diff --git a/ILSpy/TreeNodes/TypeReferenceTreeNode.cs b/ILSpy/TreeNodes/TypeReferenceTreeNode.cs index 83872ebe6f..36ddc15b8c 100644 --- a/ILSpy/TreeNodes/TypeReferenceTreeNode.cs +++ b/ILSpy/TreeNodes/TypeReferenceTreeNode.cs @@ -20,6 +20,7 @@ using ICSharpCode.Decompiler; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.TypeSystem; namespace ICSharpCode.ILSpy.TreeNodes { @@ -28,19 +29,21 @@ namespace ICSharpCode.ILSpy.TreeNodes /// public sealed class TypeReferenceTreeNode : ILSpyTreeNode { - readonly PEFile module; - private readonly TypeReferenceMetadata r; + readonly MetadataModule module; + readonly TypeReferenceMetadata r; + readonly IType resolvedType; - public TypeReferenceTreeNode(PEFile module, TypeReferenceMetadata r) + public TypeReferenceTreeNode(MetadataModule module, TypeReferenceMetadata r) { this.module = module ?? throw new ArgumentNullException(nameof(module)); this.r = r ?? throw new ArgumentNullException(nameof(r)); + this.resolvedType = module.ResolveType(r.Handle, default); this.LazyLoading = true; } public override object Text - => Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false) + GetSuffixString(r.Handle); + => Language.TypeToString(resolvedType, includeNamespace: false) + GetSuffixString(r.Handle); public override object Icon => Images.Class; @@ -57,7 +60,7 @@ protected override void LoadChildren() public override void Decompile(Language language, ITextOutput output, DecompilationOptions options) { - language.WriteCommentLine(output, $"{Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false)}"); + language.WriteCommentLine(output, Language.TypeToString(resolvedType, includeNamespace: true)); EnsureLazyChildren(); foreach (ILSpyTreeNode child in Children) {