2 using System
.Collections
;
3 using System
.Reflection
;
4 using System
.Collections
.Generic
;
6 namespace Castle
.DynamicProxy
.Generators
8 public class MethodSignatureComparer
: IEqualityComparer
<MethodInfo
>
10 public static readonly MethodSignatureComparer Instance
= new MethodSignatureComparer ();
12 public bool Equals (MethodInfo x
, MethodInfo y
)
14 if (x
== null && y
== null)
16 else if (x
== null || y
== null)
19 return x
.Name
== y
.Name
&& EqualGenericParameters (x
, y
) && EqualSignatureTypes (x
.ReturnType
, y
.ReturnType
) && EqualParameters (x
, y
);
22 private bool EqualGenericParameters (MethodInfo x
, MethodInfo y
)
24 if (x
.IsGenericMethod
!= y
.IsGenericMethod
)
26 if (x
.IsGenericMethod
)
28 Type
[] xArgs
= x
.GetGenericArguments ();
29 Type
[] yArgs
= y
.GetGenericArguments ();
31 if (xArgs
.Length
!= yArgs
.Length
)
34 for (int i
= 0; i
< xArgs
.Length
; ++i
)
36 if (xArgs
[i
].IsGenericParameter
!= yArgs
[i
].IsGenericParameter
)
39 if (!xArgs
[i
].IsGenericParameter
&& !xArgs
[i
].Equals (yArgs
[i
]))
46 private bool EqualParameters (MethodInfo x
, MethodInfo y
)
48 ParameterInfo
[] xArgs
= x
.GetParameters ();
49 ParameterInfo
[] yArgs
= y
.GetParameters ();
51 if (xArgs
.Length
!= yArgs
.Length
)
54 for (int i
= 0; i
< xArgs
.Length
; ++i
)
56 if (!EqualSignatureTypes(xArgs
[i
].ParameterType
, yArgs
[i
].ParameterType
))
62 private static bool EqualSignatureTypes (Type x
, Type y
)
64 if (x
.IsGenericParameter
!= y
.IsGenericParameter
)
67 if (x
.IsGenericParameter
)
69 if (x
.GenericParameterPosition
!= y
.GenericParameterPosition
)
80 public int GetHashCode (MethodInfo obj
)
82 return obj
.Name
.GetHashCode () ^ obj
.GetParameters ().Length
; // everything else would be too cumbersome