如何创建复制所有匹配属性的LambdaExpression

本文关键字:属性 LambdaExpression 复制 何创建 创建 | 更新日期: 2023-09-27 18:26:45

我已经完成了基本的东西,但我一直在创建实际的LambaExpresion:

有人对我想写的内容有什么建议吗?var COPYEXPRESSION = ...

public Expression<Func<TSource, TDestination>> GetOrCreateMapExpression<TSource, TDestination>()
{
    return (Expression<Func<TSource, TDestination>>)
        _expressionCache.GetOrAdd(new TypePair(typeof(TSource), typeof(TDestination)), tp =>
        {
            return CreateMapExpression(tp.SourceType, tp.DestinationType);
        });
}
private LambdaExpression CreateMapExpression(Type source, Type destination)
{
    ParameterExpression instanceParameter = Expression.Parameter(source);

   var sourceMembers =  source.GetProperties();
   var destMembers = destination.GetProperties();
   var matchingMembers = sourceMembers.Select(s =>
       new
       {
           Source = s,
           Dest = destMembers.FirstOrDefault(d =>
               d.Name.Equals(s.Name) && d.PropertyType == s.PropertyType)
       }).Where(map => map.Dest != null).ToArray();

  var COPYEXPRESSION = ...
    return Expression.Lambda(COPYEXPRESSION , instanceParameter);
}

更新

我已经得到了正确的返回类型,但在单元测试时,映射的类的属性为null。

private LambdaExpression CreateMapExpression(Type source, Type destination)
{
    ParameterExpression instanceParameter = Expression.Parameter(source);
    var instance2Parameter = Expression.New(destination);
    LabelTarget returnTarget = Expression.Label(destination);
    var sourceMembers = source.GetProperties().Where(p => p.GetMethod.IsPublic);
    var destMembers = destination.GetProperties().Where(p => p.SetMethod.IsPublic);
    var matchingMembers = sourceMembers.Select(s =>
        new
        {
            Source = s,
            Dest = destMembers.FirstOrDefault(d =>
                d.Name.Equals(s.Name) && d.PropertyType == s.PropertyType)
        }).Where(map => map.Dest != null).ToArray();
    var block = Expression.Block(Expression.Block(
        matchingMembers.Select(p =>
            Expression.Assign(
                Expression.Property(instance2Parameter, p.Dest),
                Expression.Property(instanceParameter, p.Source)))),
                 Expression.Label(returnTarget, instance2Parameter));

    return Expression.Lambda(block, instanceParameter);
}

解决方案

这对我有效:

    return Expression.Lambda( Expression.MemberInit(Expression.New(destination),
        matchingMembers.Select(p =>
            Expression.Bind(p.Dest, Expression.Property(instanceParameter, p.Source)))),
            instanceParameter);

如何创建复制所有匹配属性的LambdaExpression

给定

ParameterExpression instanceParameter = Expression.Parameter(source);
ParameterExpression instance2Parameter = Expression.Parameter(destination);

您需要两个参数,一个用于源,另一个用于目标。。。

除非你正在构建一个新的目的地,否则你需要一个Expression.Variable来代替instance2Parameter,你将把Expression.New 放在那里

var block = Expression.Block(
                matchingMembers.Select(p =>       
                    Expression.Assign(
                        Expression.Property(instance2Parameter, p.Dest),
                        Expression.Property(instanceParameter, p.Source)))

这是一个包含所有Expression.Assign 的块

请注意,您应该检查Dest中是否存在setter,以及Source中是否存在getter(但最好在sourceMembers.Select中进行检查)。