简体   繁体   English

确定两个方法是否具有相同的基本定义

[英]Determine if two methods have the same base definition

I've just made the unfortunate (for my app at least) discovery that two methods declared inside a generic class do not have the same base definition, demonstrated best in code: 我刚刚(至少对于我的应用程序)不幸地发现,在通用类中声明的两个方法没有相同的基定义,这在代码中得到了最好的展示:

    public static class Test
    {
        private class Generic<T> { public void Method() { } }
        public static void TestBase()
        {
            var x = typeof(Generic<int>).GetMethod("Method");
            var y = typeof(Generic<double>).GetMethod("Method");
            Debug.Assert(x.GetBaseDefinition() == y.GetBaseDefinition()); // fails
        }
    }

Both x and y.IsGeneric is false, so GetGenericMethodDefinition cannot be used. x和y.IsGeneric均为false,因此无法使用GetGenericMethodDefinition。

The only solution I've been able to think of so far is to compare their names and that their declaring types are the same generic type, but in the presence of overloads that seems very brittle.. 到目前为止,我唯一能想到的解决方案是比较它们的名称,并声明它们的声明类型是相同的泛型类型,但是在存在重载的情况下,它似乎非常脆弱。

So.. I don't suppose there's a helpful method I've missed in the reflection library that can tell me if these two methods have been first declared in the same class? 所以..我不认为反射库中缺少一个有用的方法,它可以告诉我这两个方法是否已在同一类中首先声明? Or a workaround? 还是解决方法?

EDIT: 编辑:

To clarify, I want to make a method: 为了澄清,我想做一个方法:

    public bool DeclaredInSameClass(MethodInfo a, MethodInfo b);

which returns true if both a and b are both first declared in the same class. 如果a和b都首先在同一类中声明,则返回true。

Ignoring generics, this is simple: a.GetBaseDefinition() == y.GetBaseDefinition() , but how to handle methods declared within generic classes? 忽略泛型,这很简单: a.GetBaseDefinition() == y.GetBaseDefinition() ,但是如何处理在泛型类中声明的方法?

EDIT... one last try: 编辑...最后一次尝试:

    private class Generic<T> { 
        public void Method() { }
        public void Method(string param) { }
        public void OtherMethod() { }  
    }
    private class NonGeneric { public void Method() { } }
    static void Main(string[] args)
    {
        var x = typeof(Generic<int>).GetMethod("Method", new Type[]{});
        var y = typeof(Generic<double>).GetMethod("Method", new Type[]{});
        var a = typeof(Generic<double>).GetMethod("OtherMethod");
        var b = typeof(NonGeneric).GetMethod("Method");
        var c = typeof(Generic<int>).GetMethod("Method", new Type[] { typeof(string) });

        Debug.Assert(DeclaredInSameClass(x, y));
        Debug.Assert(!DeclaredInSameClass(x, a));
        Debug.Assert(!DeclaredInSameClass(x, b));
        Debug.Assert(!DeclaredInSameClass(x, c));
        Debug.Assert(!DeclaredInSameClass(a, b));

    }

    public static bool DeclaredInSameClass(MethodInfo a, MethodInfo b)
    {
        if (a.DeclaringType.IsGenericType != b.DeclaringType.IsGenericType)
        {
            return false;
        }
        else if (a.DeclaringType.IsGenericType)
        {

            var x = a.DeclaringType.GetGenericTypeDefinition().GetMethod(a.Name, a.GetParameters().Select(p => p.ParameterType).ToArray());
            var y = b.DeclaringType.GetGenericTypeDefinition().GetMethod(b.Name, b.GetParameters().Select(p => p.ParameterType).ToArray());
            return x.Equals(y);
        }
        return a.GetBaseDefinition().Equals(b.GetBaseDefinition());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道两个单词是否具有相同的基数? - How to know if two words have the same base? 如何在API控制器中有两个名称相同但参数不同的方法? - How to have two methods in API Controller with same name but different arguments? 将两种方法重构为一种具有相同字段但名称不同的方法 - Refactoring two methods into one that have the same fields but different names C#-如何使以下类成为基类,以便子类可以具有相同的属性和方法? - C# - How to make the following class a base class so that child classes can have the same properties, methods? 如果对象具有相同的基数 class,如何处理具有不同返回类型的方法? - How to handle methods with different return types if the objects have same base class? 两种具有相同行为的方法 - Two methods with the same behavior 确定两个浮点数在C#中是否具有相同符号的更优雅方法? - More elegant way to determine whether two floats have same signs in C#? 我如何串联两个不同的列表 <T> 两者都有相同的基类? - How can i concatenate two different list<T> where both have same base class? 创建两个具有相同变量但具有不同访问方法的类的最佳方法 - Best method of creating two classes that have the same variables, but with different access methods 两个具有相同名称的通用方法 - Two generic methods with the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM