Added container accessor to Castle.Core
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / Generators / MethodSignatureComparer.cs
blobb3e97ebd1d67cc6d60ff9b257e2095d0130f9581
1 using System;
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)
15 return true;
16 else if (x == null || y == null)
17 return false;
18 else
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)
25 return false;
26 if (x.IsGenericMethod)
28 Type[] xArgs = x.GetGenericArguments ();
29 Type[] yArgs = y.GetGenericArguments ();
31 if (xArgs.Length != yArgs.Length)
32 return false;
34 for (int i = 0; i < xArgs.Length; ++i)
36 if (xArgs[i].IsGenericParameter != yArgs[i].IsGenericParameter)
37 return false;
39 if (!xArgs[i].IsGenericParameter && !xArgs[i].Equals (yArgs[i]))
40 return false;
43 return true;
46 private bool EqualParameters (MethodInfo x, MethodInfo y)
48 ParameterInfo[] xArgs = x.GetParameters ();
49 ParameterInfo[] yArgs = y.GetParameters ();
51 if (xArgs.Length != yArgs.Length)
52 return false;
54 for (int i = 0; i < xArgs.Length; ++i)
56 if (!EqualSignatureTypes(xArgs[i].ParameterType, yArgs[i].ParameterType))
57 return false;
59 return true;
62 private static bool EqualSignatureTypes (Type x, Type y)
64 if (x.IsGenericParameter != y.IsGenericParameter)
65 return false;
67 if (x.IsGenericParameter)
69 if (x.GenericParameterPosition != y.GenericParameterPosition)
70 return false;
72 else
74 if (!x.Equals (y))
75 return false;
77 return true;
80 public int GetHashCode (MethodInfo obj)
82 return obj.Name.GetHashCode () ^ obj.GetParameters ().Length; // everything else would be too cumbersome