-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MethodInfo objects for same method are not equal #67989
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsDescriptionIn some cases, Reproduction StepsMWE:
Expected behaviorI expected Actual behaviorIn some cases, Regression?No response Known WorkaroundsNo response ConfigurationWindows 10 21H2 x64 Other informationNo response
|
Verified the issue; depending on how a Console.WriteLine(object.ReferenceEquals(m1, m2)); // 'false' @buyaa-n per research into #46272 is there a single cache for |
FWIW having only a single |
Yes based on this hot reload at #69427 preserving instances is probably not feasible. Supporting a proper |
Yes there is a single cache for MethodInfo, but as the cache also checks method handles for equality in this case 2 method will be added to the cache runtime/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs Lines 87 to 88 in 810a7f9
I will check how those methods are created/obtained |
I do not think that this issue and #69427 have the same root cause. Without hot-reload, we are not supposed to ever end up with multiple MethodInfos pointing to same (non-generic) method. Internally, the runtime can have multiple MethodDesc* for the same method. These MethodDescs are unboxing and/instantiating stubs. This is runtime implementation detail that is not supposed to leak through the reflection APIs. The MethodDesc* should be always normalized using I agree that changing |
Description
In some cases,
MethodInfo
objects that refer to the same method but are obtained in different ways (reflection vs delegate creation, i.e.ldftn
IL instruction) do not compare equal, and have different hash codes.DeclaringType
andMetadataToken
properties of the twoMethodInfo
s are the same, but theirMethodHandle
s are different. This happens when the method is declared in a value type, but not if it is declared in a reference type. This is very inconvenient because this means thatMethodInfo
s cannot be used as dictionary keys.Reproduction Steps
MWE:
Expected behavior
I expected
MethodInfo
objects referring to the same method to compare equal.Actual behavior
In some cases,
MethodInfo
objects that refer to the same method but are obtained in different ways (reflection vs delegate creation, i.e.ldftn
IL instruction) do not compare equal, and have different hash codes.Regression?
No response
Known Workarounds
No response
Configuration
Windows 10 21H2 x64
Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34)
This behavior is the same in
net461
(4.8.4470.0),netcoreapp3.1
(3.1.23),net5.0
(5.0.12) andnet6.0
(6.0.1).Other information
No response
The text was updated successfully, but these errors were encountered: