Skip to content

Hot Reload breaks MethodInfo equality #69427

Closed
@stakx

Description

@stakx

Description

This is very similar to #67989, but with an important difference. That other issue states that:

In some cases, MethodInfo objects that refer to the same method but are obtained in different ways [...] do not compare equal [...].

It seems that Hot Reload appears to break MethodInfo equality even when the MethodInfos are obtained in exactly the same way. See the attached repro code example.

Reproduction Steps

Here's a short console app that you'll need to run twice: once without making any code changes; and a second time by making any code change while Console.ReadLine is waiting for user input. For example, change the . in "Hot reload here." to a !, then hit Apply Code Changes (then hit enter in the app's console window).

var method1 = typeof(IInterface).GetMethod("Method")!;

Console.WriteLine("Hot reload here.");
Console.ReadLine();

var method2 = typeof(IInterface).GetMethod("Method")!;

Console.WriteLine(method1.ReflectedType == method2.ReflectedType && method1.Module == method2.Module && method1.MetadataToken == method2.MetadataToken);
Console.WriteLine(method1 == method2);
Console.WriteLine(method1.Equals(method2));

public interface IInterface
{
    void Method();
}

Expected behavior

In both cases (without & with making any code changes), the expected output of the console app should be:

True
True
True

Actual behavior

When making a code change and Hot Reload's Apply Code Changes is performed, the output will be:

True
False
False

Regression?

No response

Known Workarounds

I suppose one could do what the repro code does, and use method1.Module == method2.Module && method1.MetadataToken == method2.MetadataToken instead of method1.Equals(method2) (though not sure whether that is guaranteed to work in all cases, e.g. with generic method instantiations)... but that obviously shouldn't be necessary.

Configuration

  • .NET version: 6 (SDK version 6.0.300, commit 8473146e7d) with Visual Studio 17.2.0
  • OS: Windows 10 Pro (21H2)
  • Architecture: x64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions