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