Washi1337/AsmResolver

AsmResolver fails to resolve a method due to difference in the scope

N78750469 opened this issue · 1 comments

AsmResolver Version

4.11.1

.NET Version

.NET 6.0, .NET 5.0

Operating System

Windows

Describe the Bug

The current AsmResolver fails to resolve a method because of a difference in the scope. when comparing the signature, the return type scope will be different since the MemberReference signature uses the runtime of the main assembly whereas the candidate uses the runtime of the referenced dll.

How To Reproduce

Create two projects, The main application .NET 6.0, and the second one is the referenced dll with .NET 5.0.

  • Main application:
using ReferenceMe;

var newclass = new IamClass();

newclass.ThrowMe("HI i am an exception.");
  • Referenced Dll:
using System;

namespace ReferenceMe
{
    public class IamClass
    {
        public Exception ThrowMe(string message) => new Exception(message);
    }
}

Then resolve ThrowMe

using AsmResolver.DotNet;
using AsmResolver.PE.DotNet.Cil;

var assembly = AssemblyDefinition.FromFile(@"..\..\..\..\MainApp\bin\Debug\net6.0\MainApp.dll");

var call = assembly.ManifestModule.ManagedEntrypointMethod.CilMethodBody.Instructions.First((x) => x.OpCode == CilOpCodes.Callvirt && (x.Operand as IMethodDescriptor).Name == "ThrowMe");

if (call.Operand is MemberReference memberReference) {
    var resolved = memberReference.Resolve();
    if (resolved is null)
        throw new NullReferenceException("Failed to resovled the reference.");
}

Console.ReadLine();

Expected Behavior

It should return MethodDefinition of ThrowMe.

Actual Behavior

It returns null.

Additional Context

No response

This seems to be a bug in the DefaultMetadataResolver being to strict in the way it is comparing signatures.

We might want to consider disabling the version check in the signature comparer of the default metadata resolver. When the metadata resolver is matching types and signatures, it already is working with the resolved assembly anyway.