Skip to content
Open
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions ICSharpCode.Decompiler/Metadata/MetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,44 @@ public static bool TryGetFullAssemblyName(this SRM.AssemblyReference reference,
}
}

/// <summary>
/// True when <paramref name="reference"/> names the assembly defined by <paramref name="reader"/>:
/// same simple name (ignoring case), culture and public key token. The version is not compared,
/// since binding redirects and unified frameworks satisfy references with other versions.
/// </summary>
public static bool IsReferenceTo(this IAssemblyReference reference, MetadataReader reader)
{
if (reference == null)
throw new ArgumentNullException(nameof(reference));
if (reader == null)
throw new ArgumentNullException(nameof(reader));
if (!reader.IsAssembly)
return false;
var asm = reader.GetAssemblyDefinition();
if (!string.Equals(reference.Name, reader.GetString(asm.Name), StringComparison.OrdinalIgnoreCase))
return false;
if (!string.Equals(NormalizeCulture(reference.Culture), NormalizeCulture(reader.GetString(asm.Culture)), StringComparison.OrdinalIgnoreCase))
return false;
var token = reference.PublicKeyToken is { Length: > 0 } bytes ? bytes.ToHexString(bytes.Length) : "null";
return string.Equals(token, reader.GetPublicKeyToken(), StringComparison.OrdinalIgnoreCase);

static string NormalizeCulture(string culture)
=> string.IsNullOrEmpty(culture) || culture == "neutral" ? string.Empty : culture;
}

/// <summary>
/// True when <paramref name="reference"/> asks for a different version than the assembly
/// defined by <paramref name="reader"/>. Unknown versions never count as a mismatch.
/// </summary>
public static bool IsVersionMismatch(this IAssemblyReference reference, MetadataReader reader)
{
if (reference == null)
throw new ArgumentNullException(nameof(reference));
if (reader == null)
throw new ArgumentNullException(nameof(reader));
return reference.Version != null && reader.IsAssembly && reference.Version != reader.GetAssemblyDefinition().Version;
}

public static string ToHexString(this IEnumerable<byte> bytes, int estimatedLength)
{
if (bytes == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2026 Piero Viano
//
// 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 System.Collections.Generic;
using System.Composition;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpyX.Dependencies;

namespace ICSharpCode.ILSpyX.Analyzers.Builtin
{
/// <summary>
/// For an assembly, lists the code in the other assemblies of the list that depends on it: every
/// type or member whose signature, base types, attributes or body uses a type or member of the
/// analyzed assembly. Results are grouped by referencing assembly (assemblies in list order).
/// </summary>
[ExportAnalyzer(Header = "Dependent Code", Order = 20)]
[Shared]
class ModuleDependentCodeAnalyzer : IAnalyzer
{
public bool Show(ISymbol? symbol) => symbol is IModule { MetadataFile.IsAssembly: true };

public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext context)
{
ArgumentNullException.ThrowIfNull(analyzedSymbol);
ArgumentNullException.ThrowIfNull(context);
if (analyzedSymbol is not IModule { MetadataFile: { } target })
throw new ArgumentException("A module with metadata is required.", nameof(analyzedSymbol));
return AnalyzeCore(target, context);
}

static IEnumerable<ISymbol> AnalyzeCore(MetadataFile target, AnalyzerContext context)
{
var ct = context.CancellationToken;
var assemblies = context.AssemblyList.GetAllAssemblies().GetAwaiter().GetResult();
foreach (var assembly in assemblies)
{
ct.ThrowIfCancellationRequested();
var module = assembly.GetMetadataFileOrNull();
if (module == null || module.IsMetadataOnly || ReferenceEquals(module, target))
continue;
var references = DependentCodeScanner.FindReferencesTo(module, target);
if (references.Count == 0)
continue;
var typeSystem = context.GetOrCreateTypeSystem(module);
foreach (var symbol in DependentCodeScanner.FindDependentSymbols(module, typeSystem, references, ct))
yield return symbol;
}
}
}
}
59 changes: 59 additions & 0 deletions ICSharpCode.ILSpyX/Analyzers/Builtin/ModuleReferencedByAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2026 Piero Viano
//
// 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 System.Collections.Generic;
using System.Composition;

using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.ILSpyX.Dependencies;

namespace ICSharpCode.ILSpyX.Analyzers.Builtin
{
/// <summary>
/// For an assembly, lists the assemblies of the list that reference it (by name and public key
/// token; the referenced version may differ). Each result is itself a module, so it can be
/// expanded to follow the chain of referencing assemblies further.
/// </summary>
[ExportAnalyzer(Header = "Referenced By", Order = 10)]
[Shared]
class ModuleReferencedByAnalyzer : IAnalyzer
{
public bool Show(ISymbol? symbol) => symbol is IModule { MetadataFile.IsAssembly: true };

public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext context)
{
ArgumentNullException.ThrowIfNull(analyzedSymbol);
ArgumentNullException.ThrowIfNull(context);
if (analyzedSymbol is not IModule { MetadataFile: { } target })
throw new ArgumentException("A module with metadata is required.", nameof(analyzedSymbol));
return AnalyzeCore(target, context);
}

static IEnumerable<ISymbol> AnalyzeCore(MetadataFile target, AnalyzerContext context)
{
var assemblies = context.AssemblyList.GetAllAssemblies().GetAwaiter().GetResult();
foreach (var referencing in ReferencedByFinder.FindReferencingAssemblies(assemblies, target, context.CancellationToken))
{
context.CancellationToken.ThrowIfCancellationRequested();
yield return context.GetOrCreateTypeSystem(referencing.Module).MainModule;
}
}
}
}
Loading