MethodInfo的平等声明类型 [英] MethodInfo Equality for Declaring Type

查看:144
本文介绍了MethodInfo的平等声明类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要两个MethodInfos的检查平等。它们实际上是完全相同的MethodInfo除外ReflectedType的(即,DeclaringType是相同的,所述方法应该实际上具有相同的身体)。有一些这样的方式,但我在寻找最高效的。

I need to check equality between two MethodInfos. They are actually the exact same MethodInfo with the exception of the ReflectedType (that is, the DeclaringType is the same and the methods should actually have the same body). There are a number of ways of doing this, but I'm looking for the most efficient.

现在我有:

    public static bool AreMethodsEqualForDeclaringType(this MethodInfo first, MethodInfo second)
    {
        first = first.ReflectedType == first.DeclaringType ? first : first.DeclaringType.GetMethod(first.Name, first.GetParameters().Select(p => p.ParameterType).ToArray());
        second = second.ReflectedType == second.DeclaringType ? second : second.DeclaringType.GetMethod(second.Name, second.GetParameters().Select(p => p.ParameterType).ToArray());
        return first == second;
    }

这是一种昂贵的,所以我不知道是否有更好的方法......

This is kind of expensive, so I'm wondering if there's a better way...

我应该比较两种方法体呢?如:

Should I be comparing the two method bodies instead? eg.

first.GetMethodBody() == second.GetMethodBody()

感谢。

推荐答案

我想我会留下我的答案的问题的答案...

i guess I'll leave my answer as the answer to the question...

有一点需要注意:

first.GetMethodBody() == second.GetMethodBody()

没有工作...所以我迄今为止发现的唯一的答案是:

does NOT work...so the only answer I've found to date is:

public static bool AreMethodsEqualForDeclaringType(this MethodInfo first, MethodInfo second)
{
    first = first.ReflectedType == first.DeclaringType ? first : first.DeclaringType.GetMethod(first.Name, first.GetParameters().Select(p => p.ParameterType).ToArray());
    second = second.ReflectedType == second.DeclaringType ? second : second.DeclaringType.GetMethod(second.Name, second.GetParameters().Select(p => p.ParameterType).ToArray());
    return first == second;
}

这篇关于MethodInfo的平等声明类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆