简体   繁体   English

RuntimeMethodInfo相等:bug?

[英]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.

相关问题 获取类RuntimeMethodInfo的DefaultConstructor - Get DefaultConstructor of Class RuntimeMethodInfo Reflection.Emit:如何可靠地将MethodBuilder转换为RuntimeMethodInfo? - Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably? Winscp with SSIS包给出System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object []参数,Object []参数) - Winscp with SSIS package gives System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 带有 SSIS 包的 Winscp 给出 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters,) 错误 - Winscp with SSIS package gives System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters,) ERROR 相等性和多态性 - Equality and polymorphism 为什么 RuntimeMethodInfo.Invoke 没有出现在我的 Visual Studio 调试器调用堆栈上? - Why doesn't RuntimeMethodInfo.Invoke show up on my Visual Studio debugger callstack? 从非泛型 static class 中的泛型重载方法中获取 RuntimeMethodInfo - Get RuntimeMethodInfo from generic overloaded methods in non-generic static class 按位相等 - Bitwise equality GetHashCode Equality - GetHashCode Equality 在相等运算符实现中引用相等运算符 - Referencing equality operator within equality operator implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM