using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Swan.Reflection;
namespace Swan {
///
/// Represents a quick object comparer using the public properties of an object
/// or the public members in a structure.
///
public static class ObjectComparer {
///
/// Compare if two variables of the same type are equal.
///
/// The type of objects to compare.
/// The left.
/// The right.
/// true if the variables are equal; otherwise, false.
public static Boolean AreEqual(T left, T right) => AreEqual(left, right, typeof(T));
///
/// Compare if two variables of the same type are equal.
///
/// The left.
/// The right.
/// Type of the target.
///
/// true if the variables are equal; otherwise, false.
///
/// targetType.
public static Boolean AreEqual(Object left, Object right, Type targetType) {
if(targetType == null) {
throw new ArgumentNullException(nameof(targetType));
}
if(Definitions.BasicTypesInfo.Value.ContainsKey(targetType)) {
return Equals(left, right);
}
return targetType.IsValueType || targetType.IsArray ? AreStructsEqual(left, right, targetType) : AreObjectsEqual(left, right, targetType);
}
///
/// Compare if two objects of the same type are equal.
///
/// The type of objects to compare.
/// The left.
/// The right.
/// true if the objects are equal; otherwise, false.
public static Boolean AreObjectsEqual(T left, T right) where T : class => AreObjectsEqual(left, right, typeof(T));
///
/// Compare if two objects of the same type are equal.
///
/// The left.
/// The right.
/// Type of the target.
/// true if the objects are equal; otherwise, false.
/// targetType.
public static Boolean AreObjectsEqual(Object left, Object right, Type targetType) {
if(targetType == null) {
throw new ArgumentNullException(nameof(targetType));
}
PropertyInfo[] properties = PropertyTypeCache.DefaultCache.Value.RetrieveAllProperties(targetType).ToArray();
foreach(PropertyInfo propertyTarget in properties) {
Func