[bcl] Fix Delegate.GetHashCode () so it takes into account the target too. Fixes...
[mono.git] / mcs / class / corlib / System / Delegate.cs
index c661760e8d0ce1160546a0488ad4d674e3326ec5..120991b2f79a9de061acadac90235eaa9d4b15fc 100644 (file)
@@ -60,6 +60,7 @@ namespace System
                private object m_target;
                private IntPtr method;
                private IntPtr delegate_trampoline;
+               private IntPtr extra_arg;
                private IntPtr method_code;
                private MethodInfo method_info;
 
@@ -101,17 +102,7 @@ namespace System
 
                public MethodInfo Method {
                        get {
-                               if (method_info != null) {
-                                       return method_info;
-                               } else {
-                                       if (method != IntPtr.Zero) {
-                                               if (!method_is_virtual)
-                                                       method_info = (MethodInfo)MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (method));
-                                               else
-                                                       method_info = GetVirtualMethod_internal ();
-                                       }
-                                       return method_info;
-                               }
+                               return GetMethodImpl ();
                        }
                }
 
@@ -504,13 +495,26 @@ namespace System
 
                public override int GetHashCode ()
                {
-                       /* same implementation as CoreCLR */
-                       return GetType ().GetHashCode ();
+                       MethodInfo m;
+
+                       m = Method;
+
+                       return (m != null ? m.GetHashCode () : GetType ().GetHashCode ()) ^ (m_target != null ? m_target.GetHashCode () : 0);
                }
 
                protected virtual MethodInfo GetMethodImpl ()
                {
-                       return Method;
+                       if (method_info != null) {
+                               return method_info;
+                       } else {
+                               if (method != IntPtr.Zero) {
+                                       if (!method_is_virtual)
+                                               method_info = (MethodInfo)MethodBase.GetMethodFromHandleNoGenericCheck (new RuntimeMethodHandle (method));
+                                       else
+                                               method_info = GetVirtualMethod_internal ();
+                               }
+                               return method_info;
+                       }
                }
 
                // This is from ISerializable
@@ -533,6 +537,12 @@ namespace System
                        if (a == null)
                                return b;
 
+                       if (b == null)
+                               return a;
+
+                       if (a.GetType () != b.GetType ())
+                               throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types. First is {0} second is {1}.", a.GetType ().FullName, b.GetType ().FullName));
+
                        return a.CombineImpl (b);
                }