38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
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<PropertyValueVariance> GetPropertyValueVariances<T>(this T objectInstanceOne, T objectInstanceTwo)
|
|
{
|
|
var propertyValueVariances = new List<PropertyValueVariance>();
|
|
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<T>(this T queryObject, PropertyInfo propertyInfo)
|
|
{
|
|
var propertyValue = propertyInfo.GetValue(queryObject);
|
|
return propertyValue != null ? propertyValue.ToString() : "NULL";
|
|
}
|
|
}
|