-
-
Save kevingosse/f179d7d0e0285e5e6245bb1ba5d22e30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override int GetHashCode() | |
{ | |
Type type = GetType(); | |
FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); | |
Object vThis = null; | |
for (int i = 0; i < fields.Length; i++) | |
{ | |
// Visibility check and consistency check are not necessary. | |
Object fieldValue = ((RtFieldInfo)fields[i]).UnsafeGetValue(this); | |
// The hashcode of an array ignores the contents of the array, so it can produce | |
// different hashcodes for arrays with the same contents. | |
// Since we do deep comparisons of arrays in Equals(), this means Equals and GetHashCode will | |
// be inconsistent for arrays. Therefore, we ignore hashes of arrays. | |
if (fieldValue != null && !fieldValue.GetType().IsArray) | |
vThis = fieldValue; | |
if (vThis != null) | |
break; | |
} | |
if (vThis != null) | |
return vThis.GetHashCode(); | |
return type.GetHashCode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment