public static void DefaultHashCode() { var a1 = new ParentAttribute { Prop = 1 }; var a2 = new ParentAttribute { Prop = 42 }; var a3 = new ParentAttribute { Prop = 1 }; var d1 = new ChildAttribute { Prop = 1 }; var d2 = new ChildAttribute { Prop = 42 }; var d3 = new ChildAttribute { Prop = 1 }; var s1 = new GrandchildAttribute { Prop = 1 }; var s2 = new GrandchildAttribute { Prop = 42 }; var s3 = new GrandchildAttribute { Prop = 1 }; var f1 = new ChildAttributeWithField { Prop = 1 }; var f2 = new ChildAttributeWithField { Prop = 42 }; var f3 = new ChildAttributeWithField { Prop = 1 }; Assert.NotEqual(0, a1.GetHashCode()); Assert.NotEqual(0, a2.GetHashCode()); Assert.NotEqual(0, a3.GetHashCode()); Assert.NotEqual(0, d1.GetHashCode()); Assert.NotEqual(0, d2.GetHashCode()); Assert.NotEqual(0, d3.GetHashCode()); Assert.NotEqual(0, s1.GetHashCode()); Assert.NotEqual(0, s2.GetHashCode()); Assert.NotEqual(0, s3.GetHashCode()); Assert.Equal(0, f1.GetHashCode()); Assert.Equal(0, f2.GetHashCode()); Assert.Equal(0, f3.GetHashCode()); Assert.NotEqual(a1.GetHashCode(), a2.GetHashCode()); Assert.NotEqual(a2.GetHashCode(), a3.GetHashCode()); Assert.Equal(a1.GetHashCode(), a3.GetHashCode()); // The implementation of Attribute.GetHashCode uses reflection to // enumerate fields. On .NET core, we add `BindingFlags.DeclaredOnly` // to fix a bug where the hash code of a subclass of an attribute can // be equal to an instance of the parent class. // See https://github.com/dotnet/coreclr/pull/6240 Assert.False(s1.GetHashCode().Equals(s2.GetHashCode())); Assert.False(s2.GetHashCode().Equals(s3.GetHashCode())); Assert.Equal(s1.GetHashCode(), s3.GetHashCode()); Assert.False(d1.GetHashCode().Equals(d2.GetHashCode())); Assert.False(d2.GetHashCode().Equals(d3.GetHashCode())); Assert.Equal(d1.GetHashCode(), d3.GetHashCode()); Assert.Equal(f1.GetHashCode(), f2.GetHashCode()); Assert.Equal(f2.GetHashCode(), f3.GetHashCode()); Assert.Equal(f1.GetHashCode(), f3.GetHashCode()); Assert.True(d1.GetHashCode().Equals(a1.GetHashCode())); Assert.True(d2.GetHashCode().Equals(a2.GetHashCode())); Assert.True(d3.GetHashCode().Equals(a3.GetHashCode())); Assert.True(d1.GetHashCode().Equals(a3.GetHashCode())); Assert.True(d3.GetHashCode().Equals(a1.GetHashCode())); Assert.True(d1.GetHashCode().Equals(s1.GetHashCode())); Assert.True(d2.GetHashCode().Equals(s2.GetHashCode())); Assert.True(d3.GetHashCode().Equals(s3.GetHashCode())); Assert.True(d1.GetHashCode().Equals(s3.GetHashCode())); Assert.True(d3.GetHashCode().Equals(s1.GetHashCode())); Assert.NotEqual(f1.GetHashCode(), a1.GetHashCode()); Assert.NotEqual(f2.GetHashCode(), a2.GetHashCode()); Assert.NotEqual(f3.GetHashCode(), a3.GetHashCode()); Assert.NotEqual(f1.GetHashCode(), a3.GetHashCode()); Assert.NotEqual(f3.GetHashCode(), a1.GetHashCode()); }
public static void DefaultHashCode() { var a1 = new ParentAttribute { Prop = 1 }; var a2 = new ParentAttribute { Prop = 42 }; var a3 = new ParentAttribute { Prop = 1 }; var d1 = new ChildAttribute { Prop = 1 }; var d2 = new ChildAttribute { Prop = 42 }; var d3 = new ChildAttribute { Prop = 1 }; var s1 = new GrandchildAttribute { Prop = 1 }; var s2 = new GrandchildAttribute { Prop = 42 }; var s3 = new GrandchildAttribute { Prop = 1 }; var f1 = new ChildAttributeWithField { Prop = 1 }; var f2 = new ChildAttributeWithField { Prop = 42 }; var f3 = new ChildAttributeWithField { Prop = 1 }; Assert.NotEqual(a1.GetHashCode(), 0); Assert.NotEqual(a2.GetHashCode(), 0); Assert.NotEqual(a3.GetHashCode(), 0); Assert.NotEqual(d1.GetHashCode(), 0); Assert.NotEqual(d2.GetHashCode(), 0); Assert.NotEqual(d3.GetHashCode(), 0); Assert.NotEqual(s1.GetHashCode(), 0); Assert.NotEqual(s2.GetHashCode(), 0); Assert.NotEqual(s3.GetHashCode(), 0); Assert.Equal(f1.GetHashCode(), 0); Assert.Equal(f2.GetHashCode(), 0); Assert.Equal(f3.GetHashCode(), 0); Assert.NotEqual(a1.GetHashCode(), a2.GetHashCode()); Assert.NotEqual(a2.GetHashCode(), a3.GetHashCode()); Assert.Equal(a1.GetHashCode(), a3.GetHashCode()); Assert.NotEqual(s1.GetHashCode(), s2.GetHashCode()); Assert.NotEqual(s2.GetHashCode(), s3.GetHashCode()); Assert.Equal(s1.GetHashCode(), s3.GetHashCode()); Assert.NotEqual(d1.GetHashCode(), d2.GetHashCode()); Assert.NotEqual(d2.GetHashCode(), d3.GetHashCode()); Assert.Equal(d1.GetHashCode(), d3.GetHashCode()); Assert.Equal(f1.GetHashCode(), f2.GetHashCode()); Assert.Equal(f2.GetHashCode(), f3.GetHashCode()); Assert.Equal(f1.GetHashCode(), f3.GetHashCode()); Assert.Equal(d1.GetHashCode(), a1.GetHashCode()); Assert.Equal(d2.GetHashCode(), a2.GetHashCode()); Assert.Equal(d3.GetHashCode(), a3.GetHashCode()); Assert.Equal(d1.GetHashCode(), a3.GetHashCode()); Assert.Equal(d3.GetHashCode(), a1.GetHashCode()); Assert.Equal(d1.GetHashCode(), s1.GetHashCode()); Assert.Equal(d2.GetHashCode(), s2.GetHashCode()); Assert.Equal(d3.GetHashCode(), s3.GetHashCode()); Assert.Equal(d1.GetHashCode(), s3.GetHashCode()); Assert.Equal(d3.GetHashCode(), s1.GetHashCode()); Assert.NotEqual(f1.GetHashCode(), a1.GetHashCode()); Assert.NotEqual(f2.GetHashCode(), a2.GetHashCode()); Assert.NotEqual(f3.GetHashCode(), a3.GetHashCode()); Assert.NotEqual(f1.GetHashCode(), a3.GetHashCode()); Assert.NotEqual(f3.GetHashCode(), a1.GetHashCode()); }