Browse Source

Add exported types

pull/3092/head
Siegfried Pammer 1 year ago
parent
commit
514551ef9f
  1. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  2. 18
      ICSharpCode.Decompiler/Metadata/AssemblyReferences.cs
  3. 103
      ICSharpCode.Decompiler/Metadata/ExportedTypeMetadata.cs
  4. 7
      ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs
  5. 61
      ILSpy/TreeNodes/ExportedTypeTreeNode.cs

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -362,6 +362,7 @@ @@ -362,6 +362,7 @@
<Compile Include="IL\Transforms\TransformDisplayClassUsage.cs" />
<Compile Include="IL\Transforms\UserDefinedLogicTransform.cs" />
<Compile Include="Metadata\AssemblyReferences.cs" />
<Compile Include="Metadata\ExportedTypeMetadata.cs" />
<Compile Include="Metadata\MemberReferenceMetadata.cs" />
<Compile Include="Metadata\TypeReferenceMetadata.cs" />
<Compile Include="Metadata\CodeMappingInfo.cs" />

18
ICSharpCode.Decompiler/Metadata/AssemblyReferences.cs

@ -294,6 +294,24 @@ namespace ICSharpCode.Decompiler.Metadata @@ -294,6 +294,24 @@ namespace ICSharpCode.Decompiler.Metadata
}
}
ImmutableArray<ExportedTypeMetadata> exportedTypes;
public ImmutableArray<ExportedTypeMetadata> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedTypeMetadata(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}
public AssemblyReference(MetadataReader metadata, AssemblyReferenceHandle handle)
{
if (metadata == null)

103
ICSharpCode.Decompiler/Metadata/ExportedTypeMetadata.cs

@ -0,0 +1,103 @@ @@ -0,0 +1,103 @@
// Copyright (c) 2023 James May
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
namespace ICSharpCode.Decompiler.Metadata
{
#if !VSADDIN
/// <summary>
/// Convenience wrapper for <see cref="ExportedType"/> and <see cref="ExportedTypeHandle"/>.
/// </summary>
public sealed class ExportedTypeMetadata
{
readonly ExportedType entry;
public MetadataReader Metadata { get; }
public ExportedTypeHandle Handle { get; }
string? name;
public string Name {
get {
try
{
return name ??= Metadata.GetString(entry.Name);
}
catch (BadImageFormatException)
{
return name = $"ET:{Handle}";
}
}
}
string? @namespace;
public string Namespace {
get {
try
{
return @namespace ??= Metadata.GetString(entry.Namespace);
}
catch (BadImageFormatException)
{
return @namespace = $"namespace(ET:{Handle})";
}
}
}
public EntityHandle Implementation => entry.Implementation;
public TypeAttributes Attributes => entry.Attributes;
public bool IsForwarder => entry.IsForwarder;
public NamespaceDefinition NamespaceDefinition => Metadata.GetNamespaceDefinition(entry.NamespaceDefinition);
ImmutableArray<ExportedTypeMetadata> exportedTypes;
public ImmutableArray<ExportedTypeMetadata> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedTypeMetadata(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}
public ExportedTypeMetadata(MetadataReader metadata, ExportedTypeHandle handle)
{
Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
if (handle.IsNil)
throw new ArgumentNullException(nameof(handle));
Handle = handle;
entry = metadata.GetExportedType(handle);
}
public override string ToString() => $"{Namespace}::{Name}";
}
#endif
}

7
ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs

@ -39,16 +39,19 @@ namespace ICSharpCode.ILSpy.TreeNodes @@ -39,16 +39,19 @@ namespace ICSharpCode.ILSpy.TreeNodes
this.LazyLoading = true;
}
public override object Text => $"Referenced Types ({r.TypeReferences.Length})";
public override object Text => $"Referenced Types ({r.TypeReferences.Length + r.ExportedTypes.Length})";
public override object Icon => Images.Class;
protected override void LoadChildren()
{
foreach (var typeRef in r.TypeReferences)
this.Children.Add(new TypeReferenceTreeNode(module, typeRef));
foreach (var exportedType in r.ExportedTypes)
this.Children.Add(new ExportedTypeTreeNode(module, exportedType));
}
public override bool ShowExpander => !r.TypeReferences.IsEmpty;
public override bool ShowExpander => !r.TypeReferences.IsEmpty || !r.ExportedTypes.IsEmpty;
public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
{

61
ILSpy/TreeNodes/ExportedTypeTreeNode.cs

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// Copyright (c) 2023 James May
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.ILSpy.TreeNodes
{
/// <summary>
/// exported type within assembly reference list.
/// </summary>
public sealed class ExportedTypeTreeNode : ILSpyTreeNode
{
readonly PEFile module;
private readonly ExportedTypeMetadata r;
public ExportedTypeTreeNode(PEFile module, ExportedTypeMetadata r)
{
this.module = module ?? throw new ArgumentNullException(nameof(module));
this.r = r ?? throw new ArgumentNullException(nameof(r));
this.LazyLoading = true;
}
public override object Text
=> Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false) + GetSuffixString(r.Handle);
public override object Icon => Images.Library;
protected override void LoadChildren()
{
foreach (var exportedType in r.ExportedTypes)
this.Children.Add(new ExportedTypeTreeNode(module, exportedType));
}
public override bool ShowExpander => !r.ExportedTypes.IsEmpty;
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})");
}
}
}
Loading…
Cancel
Save