Fix Attribute.GetHashCode to match MS behavior (needed for Moonlight)
authorSebastien Pouliot <sebastien@ximian.com>
Tue, 29 Jun 2010 15:02:14 +0000 (15:02 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Tue, 29 Jun 2010 15:02:14 +0000 (15:02 -0000)
svn path=/trunk/mcs/; revision=159666

mcs/class/corlib/System/Attribute.cs
mcs/class/corlib/System/ChangeLog
mcs/class/corlib/Test/System/AttributeTest.cs
mcs/class/corlib/Test/System/ChangeLog

index 333e0e39f775ce5d20572979344c75c7eed8ca0d..c82dd8406f467b45060cdea35f55dc5f9a3e5f67 100644 (file)
@@ -254,7 +254,14 @@ namespace System
 
                public override int GetHashCode ()
                {
-                       return base.GetHashCode ();
+                       int result = TypeId.GetHashCode ();
+
+                       FieldInfo[] fields = GetType ().GetFields (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+                       foreach (FieldInfo field in fields) {
+                               object value = field.GetValue (this);
+                               result ^= value == null ? 0 : value.GetHashCode ();
+                       }
+                       return result;
                }
 
                public virtual bool IsDefaultAttribute ()
index 79ec1297ed2ff4393c1983bc3e368bfcc0830411..0b57e739377d1815eb351d78ea3de67330e96b7e 100644 (file)
@@ -1,3 +1,8 @@
+2010-06-29  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * Attribute.cs: Fix GetHashCode to match MS behavior (needed for
+       Moonlight)
+
 2010-06-25  Zoltan Varga  <vargaz@gmail.com>
 
        * Array.cs (SortImpl): Fix the 'fast path' which used 'as' to cast objects so
index 9a4271672375875f4b5818b2e8e5bbca2542931d..dddf0aff7cc5f1aa34f6b156e797d3c41cf696ba 100644 (file)
@@ -954,6 +954,43 @@ namespace MonoTests.System
                private class ClassC : ClassB
                {
                }
+
+               [Test]
+               public void EmptyNonOverridenGetHashCode ()
+               {
+                       MyAttribute a1 = new MyAttribute ();
+                       MyAttribute a2 = new MyAttribute ();
+                       Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical argument-less");
+                       Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "Empty/TypeId");
+
+                       MySubAttribute b1 = new MySubAttribute ();
+                       Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
+                       Assert.AreEqual (b1.GetHashCode (), b1.TypeId.GetHashCode (), "Empty/TypeId/Sub");
+               }
+
+               class MyOwnCustomAttribute : MyCustomAttribute {
+
+                       public MyOwnCustomAttribute (string s)
+                               : base (s)
+                       {
+                       }
+               }
+
+               [Test]
+               public void NonEmptyNonOverridenGetHashCode ()
+               {
+                       MyCustomAttribute a1 = new MyCustomAttribute (null);
+                       MyCustomAttribute a2 = new MyCustomAttribute (null);
+                       Assert.AreEqual (a1.GetHashCode (), a2.GetHashCode (), "identical arguments");
+                       Assert.AreEqual (a1.GetHashCode (), a1.TypeId.GetHashCode (), "TypeId");
+
+                       MyCustomAttribute a3 = new MyCustomAttribute ("a");
+                       MyCustomAttribute a4 = new MyCustomAttribute ("b");
+                       Assert.AreNotEqual (a3.GetHashCode (), a4.GetHashCode (), "non-identical-arguments");
+
+                       MyOwnCustomAttribute b1 = new MyOwnCustomAttribute (null);
+                       Assert.AreNotEqual (a1.GetHashCode (), b1.GetHashCode (), "non-identical-types");
+               }
        }
 
        namespace ParamNamespace {
index 2286e155d6738975cd4104e5a3d4ec6c810bed16..ffc9f8e47838e12fbba94a8840572b008c5a3bd2 100644 (file)
@@ -1,3 +1,7 @@
+2010-06-29  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * AttributeTest.cs: Add test cases for GetHashCode
+
 2010-06-25  Zoltan Varga  <vargaz@gmail.com>
 
        * ArrayTest.cs: Add a test for #616416.