开发者

Determine if two methods have the same base definition

开发者 https://www.devze.com 2023-03-03 19:59 出处:网络
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:

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");
      开发者_JAVA百科      Debug.Assert(x.GetBaseDefinition() == y.GetBaseDefinition()); // fails
        }
    }

Both x and y.IsGeneric is false, so GetGenericMethodDefinition cannot be used.

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.

Ignoring generics, this is simple: a.GetBaseDefinition() == y.GetBaseDefinition(), but how to handle methods declared within generic classes?


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());
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消