Skip to content

Commit

Permalink
Enhance assembly name reference resolution in ModuleDefinition
Browse files Browse the repository at this point in the history
- Improve TryGetAssemblyNameReference to handle direct and forwarded assembly references.
- Implement helper methods for resolving direct and forwarded assembly references.
  • Loading branch information
Denis Kudelin committed Nov 12, 2023
1 parent 56d4409 commit 737f446
Showing 1 changed file with 56 additions and 7 deletions.
63 changes: 56 additions & 7 deletions Mono.Cecil/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,21 +756,70 @@ public static void CheckModule (ModuleDefinition module)

public static bool TryGetAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference)
{
var references = module.AssemblyReferences;

for (int i = 0; i < references.Count; i++) {
var reference = references [i];
if (!Equals (name_reference, reference))
continue;
// Try to resolve the assembly using direct reference first
if (module.TryGetDirectAssemblyNameReference (name_reference, out assembly_reference)) {
return true;
}

assembly_reference = reference;
// If direct resolution fails, try to resolve using forwarded types
if (module.TryGetForwardedAssemblyNameReference (name_reference, out assembly_reference)) {
return true;
}

// If resolution fails, set the output reference to null
assembly_reference = null;
return false;
}

static bool TryGetDirectAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference)
{
// Check each assembly reference in the module for a match by full name
foreach (var reference in module.AssemblyReferences) {
if (reference.FullName == name_reference.FullName) {
assembly_reference = reference;
return true;
}
}

// If no direct reference is found, set the output reference to null
assembly_reference = null;
return false;
}

static bool TryGetForwardedAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference nameReference, out AssemblyNameReference assemblyReference)
{
// Initialize the output parameter to null
assemblyReference = null;

// Iterate through all assembly references
foreach (var asm_ref in module.AssemblyReferences) {
AssemblyDefinition resolved_assembly;
try {
// Attempt to resolve the assembly reference
resolved_assembly = module.AssemblyResolver.Resolve (asm_ref);
}
catch (AssemblyResolutionException) {
// Skip the assembly if resolution fails
continue;
}

// Check exported types for type forwarding within the assembly
foreach (ModuleDefinition module_def in resolved_assembly.Modules) {
foreach (ExportedType exported_type in module_def.ExportedTypes) {
// Check if the exported type has a scope that matches the target assembly name
var scope = exported_type.Scope as AssemblyNameReference;
if (scope != null && Equals (scope, nameReference)) {
// If a match is found, return the assembly reference from which the type was forwarded
assemblyReference = asm_ref;
return true;
}
}
}
}

return false;
}

static bool Equals (byte [] a, byte [] b)
{
if (ReferenceEquals (a, b))
Expand Down

0 comments on commit 737f446

Please sign in to comment.