Man*_*nia 2 .net c# generics reflection
我刚刚发现不幸(至少我的应用程序)发现在泛型类中声明的两个方法没有相同的基本定义,在代码中表现最好:
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
}
}
Run Code Online (Sandbox Code Playgroud)
x和y.IsGeneric都是false,因此不能使用GetGenericMethodDefinition.
我已经能够想象的,到目前为止唯一的解决办法是比较他们的名字,他们的声明类型相同泛型类型,但在过载的情况下,似乎很脆..
那么..我不认为我在反射库中错过了一个有用的方法,可以告诉我这两个方法是否已在同一个类中首次声明?还是一个解决方法?
编辑:
为了澄清,我想制作一个方法:
public bool DeclaredInSameClass(MethodInfo a, MethodInfo b);
Run Code Online (Sandbox Code Playgroud)
如果a和b都是在同一个类中首次声明,则返回true.
忽略泛型,这很简单:a.GetBaseDefinition() == y.GetBaseDefinition()但是如何处理泛型类中声明的方法?
编辑......最后一次尝试:
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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |