Created
November 14, 2012 18:53
-
-
Save dgroft/4073985 to your computer and use it in GitHub Desktop.
Immutable object base class
This file contains hidden or 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
using System.Linq; | |
public abstract class DataObject | |
{ | |
public override bool Equals(object obj) | |
{ | |
var dataObj = obj as DataObject; | |
if (dataObj == null) return false; | |
return GetType().GetFields().Where(x => x.IsPublic).All(field => field.GetValue(this).Equals(field.GetValue(dataObj))); | |
} | |
public override int GetHashCode() | |
{ | |
return GetType().GetFields().Where(x => x.IsPublic).Aggregate(17, (current, field) => current * 31 + field.GetValue(this).GetHashCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment