如何替换表达式树中的属性类型及其值
本文关键字:属性 类型 何替换 替换 表达式 | 更新日期: 2023-09-27 18:18:01
我有一个类PersonDTO与空DateTime属性:
public class PersonDTO
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
// YYYYMMDD format
public virtual Nullable<int> Birthday { get; set; }
}
和表示层中的一个类:
public class PersonViewModel
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual Nullable<DateTime> Birthday { get; set; }
}
在我的表单上,我有两个方法负责创建Expression<Func<PersonViewModel, bool>>
对象:
private Expression<Func<PersonViewModel, bool>> GetFilterExpression()
{
Expression condition = null;
ParameterExpression pePerson = Expression.Parameter(typeof(PersonViewModel), "person");
//...
if (dtpBirth.Format != DateTimePickerFormat.Custom)
{
Expression target = Expression.Property(pePerson, pePerson.Type.GetProperty("Birthday", typeof(DateTime?)));
UnaryExpression date = Expression.Convert(Expression.Constant(dtpBirth.Value.Date), typeof (DateTime?));
condition = (condition == null)
? Expression.GreaterThan(target, date)
: Expression.And(condition, Expression.GreaterThan(target, date));
}
// Формируем лямбду с условием и возвращаем результат сформированного фильтра
return condition != null ? Expression.Lambda<Func<PersonViewModel, bool>>(condition, pePerson) : null;
}
我也在使用AutoMapper?将一个Expression<Func<PersonViewModel, bool>>
转化为Expression<Func<PersonDTO, bool>>
。转换的代码如下:
// ...
Mapper.CreateMap<PersonViewModel, PersonDTO>()
.ForMember(dto => dto.Birthday, opt => opt.MapFrom(model => model.BirthdaySingle.NullDateTimeToNullInt("yyyyMMdd")));
// ...
public static class DataTypesExtensions
{
public static DateTime? NullIntToNullDateTime(this int? input, string format)
{
if (input.HasValue)
{
DateTime result;
if (DateTime.TryParseExact(input.Value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}
}
return null;
}
//...
}
我的表达式转换器看起来像:
public static Expression<Func<TDestination, TResult>> RemapForType<TSource, TDestination, TResult>(
this Expression<Func<TSource, TResult>> expression)
{
var newParameter = Expression.Parameter(typeof(TDestination));
var visitor = new AutoMapVisitor<TSource, TDestination>(newParameter);
var remappedBody = visitor.Visit(expression.Body);
if (remappedBody == null)
{
throw new InvalidOperationException("Unable to remap expression");
}
return Expression.Lambda<Func<TDestination, TResult>>(remappedBody, newParameter);
}
public class AutoMapVisitor<TSource, TDestination> : ExpressionVisitor
{
private readonly ParameterExpression _newParameter;
private readonly TypeMap _typeMap = Mapper.FindTypeMapFor<TSource, TDestination>();
public AutoMapVisitor(ParameterExpression newParameter)
{
_newParameter = newParameter;
}
protected override Expression VisitMember(MemberExpression node)
{
var propertyMaps = _typeMap.GetPropertyMaps();
// Find any mapping for this member
// Here I think is a problem, because if it comes (person.Birthday => Convert(16.11.2016 00:00:00)) it can't find it.
var propertyMap = propertyMaps.SingleOrDefault(map => map.SourceMember == node.Member);
if (propertyMap == null)
{
return base.VisitMember(node);
}
var destinationProperty = propertyMap.DestinationProperty;
var destinationMember = destinationProperty.MemberInfo;
// Check the new member is a property too
var property = destinationMember as PropertyInfo;
if (property == null)
{
return base.VisitMember(node);
}
// Access the new property
var newPropertyAccess = Expression.Property(_newParameter, property);
return base.VisitMember(newPropertyAccess);
}
}
我需要以某种方式转换lambda表达式的一部分:person => person.Birthday > Convert(15.11.2016 00:00)
(在这种情况下,人是PersonViewModel和生日类型DateTime?)看起来像:person => person.Birthday > 20161115
(在这种情况下,人是PersonDTO和生日类型int?)。没有这个问题,一切都映射和工作正确。我知道我需要深入到树中并做一些操作,但是我不明白我应该如何以及在哪里做这些。
我会使用sg来调整二进制表达式的datetime值:
class AutoMapVisitor<TSource, TDestination>: ExpressionVisitor
{
// your stuff
protected override Expression VisitBinary(BinaryExpression node)
{
var memberNode = IsBirthdayNode(node.Left)
? node.Left
: IsBirthdayNode(node.Right)
? node.Right
: null;
if (memberNode != null)
{
var valueNode = memberNode == node.Left
? node.Right
: node.Left;
// get the value
var valueToChange = (int?)getValueFromNode(valueNode);
var leftIsMember = memberNode == node.Left;
var newValue = Expression.Constant(DataTypesExtensions.NullIntToNullDateTime(valueToChange, /*insert your format here */ ""));
var newMember = Visit(memberNode);
return Expression.MakeBinary(node.NodeType, leftIsMember ? newMember : newValue, leftIsMember ? newValue : newMember); // extend this if you have a special comparer or sg
}
return base.VisitBinary(node);
}
private bool IsBirthdayNode(Expression ex)
{
var memberEx = ex as MemberExpression;
return memberEx != null && memberEx.Member.Name == "Birthday" && memberEx.Member.DeclaringType == typeof(PersonViewModel);
}
private object getValueFromNode(Expression ex)
{
var constant = ex as ConstantExpression;
if (constant != null)
return constant.Value;
var cast = ex as UnaryExpression;
if (cast != null && ex.NodeType == ExpressionType.Convert)
return getValueFromNode(cast.Operand);
// here you can add more shortcuts to improve the performance of the worst case scenario, which is:
return Expression.Lambda(ex).Compile().DynamicInvoke(); // this will throw an exception, if you have references to other parameters in your ex
}
}
它是相当具体的,但你得到的想法,你可以使它更通用为您的用例。
但是我认为你对属性的映射是错误的。在sql中,您希望使用int比较。上面的代码已经为您完成了。当automapper改变你的属性时,它应该只是用新的替换旧的生日(改变类型),而不需要调用NullDateTimeToNullInt。上面的代码将处理比较的类型更改。如果你在匿名选择或其他地方有成员,我相信你仍然会有问题…