Skip to content

Instantly share code, notes, and snippets.

@dgroft
Created November 14, 2012 18:53
Show Gist options
  • Save dgroft/4073985 to your computer and use it in GitHub Desktop.
Save dgroft/4073985 to your computer and use it in GitHub Desktop.
Immutable object base class
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