Skip to content

Instantly share code, notes, and snippets.

@awright18
Created February 1, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awright18/935ad66db9e5863f5d7befd8c8f8f3b9 to your computer and use it in GitHub Desktop.
Save awright18/935ad66db9e5863f5d7befd8c8f8f3b9 to your computer and use it in GitHub Desktop.
namespace EqualityComparer
{
using System.Collections;
using System.Collections.Generic;
public class GenericEqualityComparer<T> : IEqualityComparer<T>
{
public static GenericEqualityComparer<T> Create()
{
return new GenericEqualityComparer<T>();
}
/// <summary>Determines whether the specified objects are equal.</summary>
/// <returns>true if the specified objects are equal; otherwise, false.</returns>
/// <param name="x">The first object of type <paramref name="T" /> to compare.</param>
/// <param name="y">The second object of type <paramref name="T" /> to compare.</param>
public bool Equals(T x, T y)
{
var comparer = GenericEqualityComparer.Create();
return comparer.Equals(x,y);
}
/// <summary>Returns a hash code for the specified object.</summary>
/// <returns>A hash code for the specified object.</returns>
/// <param name="obj">The <see cref="T:System.Object" /> for which a hash code is to be returned.</param>
/// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj" /> is a reference type and <paramref name="obj" /> is null.</exception>
public int GetHashCode(T obj)
{
return 0;
}
}
public class GenericEqualityComparer : IEqualityComparer
{
public static GenericEqualityComparer Create()
{
return new GenericEqualityComparer();
}
/// <summary>Determines whether the specified objects are equal.</summary>
/// <returns>true if the specified objects are equal; otherwise, false.</returns>
/// <param name="x">The first object to compare.</param>
/// <param name="y">The second object to compare.</param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="x" /> and <paramref name="y" /> are of different types and neither one can handle comparisons with the other.</exception>
public bool Equals(object x, object y)
{
var xProperties = x.GetType().GetProperties();
foreach (var xProperty in xProperties)
{
var yProperty = y.GetType().GetProperty(xProperty.Name);
if (yProperty == null)
{
return false;
}
var xPropertyValue = xProperty.GetValue(x, null);
var yPropertyValue = yProperty.GetValue(y, null);
if (xPropertyValue == null && yPropertyValue == null)
{
continue;
}
if (xPropertyValue == null || yPropertyValue == null)
{
return false;
}
if (!xPropertyValue.Equals(yPropertyValue))
{
return false;
}
}
var xFields = x.GetType().GetFields();
foreach (var xfield in xFields)
{
var yField = y.GetType().GetField(xfield.Name);
if (yField == null)
{
return false;
}
var xFieldValue = xfield.GetValue(x);
var yFieldValue = yField.GetValue(y);
if (xFieldValue == null && yFieldValue == null)
{
continue;
}
if (xFieldValue == null || yFieldValue == null)
{
return false;
}
if (!xFieldValue.Equals(yFieldValue))
{
return false;
}
}
return true;
}
/// <summary>Returns a hash code for the specified object.</summary>
/// <returns>A hash code for the specified object.</returns>
/// <param name="obj">The <see cref="T:System.Object" /> for which a hash code is to be returned.</param>
/// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj" /> is a reference type and <paramref name="obj" /> is null.</exception>
public int GetHashCode(object obj)
{
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment