Skip to content

Commit 737f446

Browse files
author
Denis Kudelin
committed
Enhance assembly name reference resolution in ModuleDefinition
- Improve TryGetAssemblyNameReference to handle direct and forwarded assembly references. - Implement helper methods for resolving direct and forwarded assembly references.
1 parent 56d4409 commit 737f446

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

Mono.Cecil/Import.cs

+56-7
Original file line numberDiff line numberDiff line change
@@ -756,21 +756,70 @@ public static void CheckModule (ModuleDefinition module)
756756

757757
public static bool TryGetAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference)
758758
{
759-
var references = module.AssemblyReferences;
760-
761-
for (int i = 0; i < references.Count; i++) {
762-
var reference = references [i];
763-
if (!Equals (name_reference, reference))
764-
continue;
759+
// Try to resolve the assembly using direct reference first
760+
if (module.TryGetDirectAssemblyNameReference (name_reference, out assembly_reference)) {
761+
return true;
762+
}
765763

766-
assembly_reference = reference;
764+
// If direct resolution fails, try to resolve using forwarded types
765+
if (module.TryGetForwardedAssemblyNameReference (name_reference, out assembly_reference)) {
767766
return true;
768767
}
769768

769+
// If resolution fails, set the output reference to null
770+
assembly_reference = null;
771+
return false;
772+
}
773+
774+
static bool TryGetDirectAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference name_reference, out AssemblyNameReference assembly_reference)
775+
{
776+
// Check each assembly reference in the module for a match by full name
777+
foreach (var reference in module.AssemblyReferences) {
778+
if (reference.FullName == name_reference.FullName) {
779+
assembly_reference = reference;
780+
return true;
781+
}
782+
}
783+
784+
// If no direct reference is found, set the output reference to null
770785
assembly_reference = null;
771786
return false;
772787
}
773788

789+
static bool TryGetForwardedAssemblyNameReference (this ModuleDefinition module, AssemblyNameReference nameReference, out AssemblyNameReference assemblyReference)
790+
{
791+
// Initialize the output parameter to null
792+
assemblyReference = null;
793+
794+
// Iterate through all assembly references
795+
foreach (var asm_ref in module.AssemblyReferences) {
796+
AssemblyDefinition resolved_assembly;
797+
try {
798+
// Attempt to resolve the assembly reference
799+
resolved_assembly = module.AssemblyResolver.Resolve (asm_ref);
800+
}
801+
catch (AssemblyResolutionException) {
802+
// Skip the assembly if resolution fails
803+
continue;
804+
}
805+
806+
// Check exported types for type forwarding within the assembly
807+
foreach (ModuleDefinition module_def in resolved_assembly.Modules) {
808+
foreach (ExportedType exported_type in module_def.ExportedTypes) {
809+
// Check if the exported type has a scope that matches the target assembly name
810+
var scope = exported_type.Scope as AssemblyNameReference;
811+
if (scope != null && Equals (scope, nameReference)) {
812+
// If a match is found, return the assembly reference from which the type was forwarded
813+
assemblyReference = asm_ref;
814+
return true;
815+
}
816+
}
817+
}
818+
}
819+
820+
return false;
821+
}
822+
774823
static bool Equals (byte [] a, byte [] b)
775824
{
776825
if (ReferenceEquals (a, b))

0 commit comments

Comments
 (0)