[英]RuntimeMethodInfo equality: bug?
Lets start with: 让我们开始:
using System;
public class Program
{
class A
{
public virtual void Do() { }
}
class B:A
{
}
public static void Main()
{
var m1 = typeof(A).GetMethod("Do");
var m2 = typeof(B).GetMethod("Do");
Console.WriteLine("Methods are equal?\t\t{0}", m1 == m2);
Console.WriteLine("Method handles are equal?\t{0}", m1.MethodHandle == m2.MethodHandle);
Console.WriteLine("Done.");
Console.ReadKey();
}
}
( try it online at ideone) (在ideone 在线试试)
So, there are two unequal MethodInfo
instances, both containing the same method handle. 因此,有两个不相等的
MethodInfo
实例,两个实例都包含相同的方法句柄。 Here's the equals operator source : 这是equals运算符源 :
public static bool operator ==(MethodInfo left, MethodInfo right)
{
if (ReferenceEquals(left, right))
return true;
if ((object)left == null || (object)right == null ||
left is RuntimeMethodInfo || right is RuntimeMethodInfo) // <----???
{
return false;
}
return left.Equals(right);
}
It doesn't look like an accidental bug, at least until there was assumption that all instances of RuntimeMethodInfo
are cached and there newer will be two different instances for the same method. 它看起来不像是一个偶然的错误,至少在假设
RuntimeMethodInfo
所有实例都被缓存并且更新的将是同一方法的两个不同实例之前。 In that case something is broken, obviously. 在那种情况下,显然有些东西被打破了。
Any reasons behind this behavior, anyone? 这种行为背后的任何原因,任何人?
PS Do not mark as a [duplicate], please:) The question is not about 'how to compare?'. PS请勿标记为[重复],请:)问题不是'如何比较?'。 That one was answered multiple times, here and here for example.
多次回答,例如这里和这里 。
Thanks! 谢谢!
I believe your assumption for the reasoning behind it - that two RuntimeMethodInfo
instances can be compared by reference equality - is correct. 我相信你对它背后的推理的假设 - 两个
RuntimeMethodInfo
实例可以通过引用相等来比较 - 是正确的。 Your assumption that it's broken isn't correct though. 你认为它被打破的假设是不正确的。
The two MethodInfo
objects here are different, as they have different ReflectedType
properties: 这里的两个
MethodInfo
对象是不同的,因为它们具有不同的ReflectedType
属性:
Console.WriteLine(m1.ReflectedType); // Program+A
Console.WriteLine(m2.ReflectedType); // Program+B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.