using LCU.Library.Internal.IncentiveApiModels.Common; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace LCU.API.Internal.Operations.IncentiveApiLib.Extensions; public static class ObjectExtensions { public static IEnumerable GetPropertyValueVariances(this T objectInstanceOne, T objectInstanceTwo) { var propertyValueVariances = new List(); var propertyInfoCollection = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(property => property.GetCustomAttributes().Any(attribute => attribute is UpdateAllowed)); foreach (var propertyInfo in propertyInfoCollection) { if (!Equals(propertyInfo.GetValue(objectInstanceOne), propertyInfo.GetValue(objectInstanceTwo))) { propertyValueVariances.Add(new PropertyValueVariance { ComparedProperty = propertyInfo.Name, PropertyValueOne = GetPropertyValueOrDefaultString(objectInstanceOne, propertyInfo), PropertyValueTwo = GetPropertyValueOrDefaultString(objectInstanceTwo, propertyInfo), }); } } return propertyValueVariances; } private static string GetPropertyValueOrDefaultString(this T queryObject, PropertyInfo propertyInfo) { var propertyValue = propertyInfo.GetValue(queryObject); return propertyValue != null ? propertyValue.ToString() : "NULL"; } }