88 lines
3.7 KiB
C#
88 lines
3.7 KiB
C#
|
using System.Linq.Expressions;
|
|||
|
|
|||
|
namespace MSAdminUsuarios.Utils
|
|||
|
{
|
|||
|
public static class GenericTools
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Permite clonar los valores de un objeto recorriendo cada parámetro
|
|||
|
/// (<paramref name="Origin"/>,<paramref name="Destiny"/>,<paramref name="exceptions"/>).
|
|||
|
/// </summary>
|
|||
|
/// <param name="Origin"> Objeto el cual se va a leer sus propiedades y valores</param>
|
|||
|
/// <param name="Destiny"> Objeto el cual se va a ESCRIBIR los valores del origen basado en el nombre de la propiedad.</param>
|
|||
|
/// <param name="exceptions"> Lista con nombres de propiedades a excluir de la copia</param>
|
|||
|
public static void CloneObj<T>(T origin, T destiny, params Expression<Func<T, object>>[] exceptions) where T : class
|
|||
|
{
|
|||
|
var destinyProps = typeof(T).GetProperties();
|
|||
|
var exceptionProps = new HashSet<string>();
|
|||
|
if (exceptions != null)
|
|||
|
{
|
|||
|
foreach (var exception in exceptions)
|
|||
|
{
|
|||
|
if (exception.Body is MemberExpression memberExpression)
|
|||
|
{
|
|||
|
exceptionProps.Add(memberExpression.Member.Name);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
foreach (var prop in typeof(T).GetProperties())
|
|||
|
{
|
|||
|
var originName = prop.Name;
|
|||
|
if (exceptionProps.Contains(originName))
|
|||
|
continue;
|
|||
|
|
|||
|
for (int i = 0; i < destinyProps.Length; i++)
|
|||
|
{
|
|||
|
if (destinyProps[i].Name == originName)
|
|||
|
{
|
|||
|
destinyProps[i].SetValue(destiny, prop.GetValue(origin, null));
|
|||
|
i = destinyProps.Length + 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Permite tomar parcialmente la información de un obejto Origen, validar si existe en el destino y si es diferente de nulo, para setearle el valor asignado
|
|||
|
/// (<paramref name="Origin"/>,<paramref name="Destiny"/>,<paramref name="exceptions"/>).
|
|||
|
/// </summary>
|
|||
|
/// <param name="Origin"> Objeto el cual se va a leer sus propiedades y valores</param>
|
|||
|
/// <param name="Destiny"> Objeto el cual se va a ESCRIBIR los valores del origen basado en el nombre de la propiedad.</param>
|
|||
|
/// <param name="exceptions"> Lista con nombres de propiedades a excluir de la copia</param>
|
|||
|
public static void ClonePartialObj<TFrom, TTo>(TFrom origin, TTo destiny, params Expression<Func<TTo, object>>[] exceptions) where TTo : class
|
|||
|
{
|
|||
|
var destinyProps = typeof(TTo).GetProperties();
|
|||
|
var exceptionProps = new HashSet<string>();
|
|||
|
if (exceptions != null)
|
|||
|
{
|
|||
|
foreach (var exception in exceptions)
|
|||
|
{
|
|||
|
if (exception.Body is MemberExpression memberExpression)
|
|||
|
{
|
|||
|
exceptionProps.Add(memberExpression.Member.Name);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
foreach (var prop in typeof(TFrom).GetProperties())
|
|||
|
{
|
|||
|
var originName = prop.Name;
|
|||
|
var originValue = prop.GetValue(origin, null);
|
|||
|
if (exceptionProps.Contains(originName))
|
|||
|
continue;
|
|||
|
|
|||
|
for (int i = 0; i < destinyProps.Length; i++)
|
|||
|
{
|
|||
|
if (destinyProps[i].Name == originName && originValue != null)
|
|||
|
{
|
|||
|
destinyProps[i].SetValue(destiny, originValue);
|
|||
|
i = destinyProps.Length + 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|