using System.Linq.Expressions; using System.Reflection; namespace StabilityMatrix.Core.Helper; public static class Expressions { public static (string propertyName, Expression> assigner) GetAssigner(Expression> propertyAccessor) { if (propertyAccessor.Body is not MemberExpression memberExpression) { throw new ArgumentException( $"Expression must be a member expression, not {propertyAccessor.Body.NodeType}"); } var propertyInfo = memberExpression.Member as PropertyInfo; if (propertyInfo == null) { throw new ArgumentException( $"Expression member must be a property, not {memberExpression.Member.MemberType}"); } var propertyName = propertyInfo.Name; var typeParam = Expression.Parameter(typeof(T)); var valueParam = Expression.Parameter(typeof(TValue)); var expr = Expression.Lambda>( Expression.Assign( Expression.MakeMemberAccess(typeParam, propertyInfo), valueParam), typeParam, valueParam); return (propertyName, expr); } }