using System;
namespace ChadSoft.Data
{
///
/// Compares all properties of a class.
///
public class PropertyComparisonCriteria : ICriteria
{
protected readonly object instance;
public PropertyComparisonCriteria(object instance)
{
this.instance = instance;
}
///
/// Does obj meet the criteria?
///
///Object to evaluate
///Whether or not obj meets the criteria.
public bool IsMatch(object x)
{
return IsMatch(instance, x);
}
///
/// Does obj meet the criteria?
///
///Object to evaluate
///Whether or not obj meets the criteria.
public static bool IsMatch(object x, object y)
{
if(x == null || y == null) return false;
var properties = x.GetType().GetProperties();
foreach (var prop in properties)
{
var iVal = prop.GetValue(x, null);
// Only compare properties that have a value set
if(iVal == null) continue;
else if(iVal is int && (int)iVal == default(int)) continue;
else if (iVal is decimal && (decimal)iVal == default(decimal)) continue;
else if (iVal is DateTime && (DateTime)iVal == default(DateTime)) continue;
if (iVal != prop.GetValue(y, null))
return false;
}
return true;
}
}
}