MyBookmark/Modules.Library.Domain/Utils/ExpressionUtils.cs
2024-09-23 03:00:50 +03:00

45 lines
1.7 KiB
C#

// Copyright (c) Philipp Wagner. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Modules.Library.Domain.Entities;
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace Modules.Library.Domain.Utils;
public static class ExpressionUtils
{
//public static void SetPropertyValue<T, TValue>(this T target, Expression<Func<T, TValue>> memberLamda, TValue value)
//{
// var memberSelectorExpression = memberLamda.Body as MemberExpression;
// if (memberSelectorExpression != null)
// {
// var property = memberSelectorExpression.Member as PropertyInfo;
// if (property != null)
// {
// property.SetValue(target, value, null);
// }
// }
//}
public static Action<TEntity, TProperty> SetProperty<TEntity, TProperty, R>(TEntity entity, Expression<Func<TEntity, TProperty>> e, TProperty value)
{
var expr = Expression.Assign(e.Body, Expression.Constant(value));
LambdaExpression lambda = Expression.Lambda(typeof(Action<TEntity, TProperty>), expr, e.Parameters[0]);
return (Action<TEntity, TProperty>)lambda.Compile();
}
//public static Expression<Action<TEntity, TProperty>> SetPropertyExpresion<TEntity, TProperty, R>(TEntity entity, Expression<Func<TEntity, TProperty>> e, TProperty value)
//{
// var expr = Expression.Assign(e.Body, Expression.Constant(value));
// return Expression.Lambda(typeof(Action<TEntity, TProperty>), expr, e.Parameters[0]);
//}
//public static Action SetEntityValues<TEntity>(TEntity entity) where TEntity : Entity
//{
//}
}