简体   繁体   English

我可以在分配属性时使用执行 lambda 表达式的结果吗?

[英]Can I use result of executing lambda expression in assigning property?

I'm writing a mapper for models, and when I need to assign values to properties of type string from any other types I'd like to call the ToString() method in them, get the value, and assign it to the target property.我正在为模型编写映射器,当我需要为任何其他类型的字符串类型的属性分配值时,我想在其中调用ToString()方法,获取值并将其分配给目标属性. My code looks something like this:我的代码看起来像这样:

public class Foo
{
    public Guid Id { get; set; }
}

public class Bar
{
    public string Id { get; set; }
}

Mapping lambda expression should looks like:映射 lambda 表达式应如下所示:

...
var $1 = new Bar();
$1.Id = parameter.Id.ToString();
...

A working version of how to do this:如何执行此操作的工作版本:

private Expression<Func<TSource, TDestination>> GenerateMapperFunc<TSource, TDestination>()
    {
        var sourceProperties = typeof(TSource)
                                .GetProperties()
                                .Where(x => x.CanRead);
        var targetProperties = typeof(TDestination)
                                .GetProperties()
                                .Where(x => x.CanWrite)
                                .ToDictionary(x => x.Name, x => x, StringComparer.OrdinalIgnoreCase);

        var source = Expression.Parameter(typeof(TSource), "source");
        var target = Expression.Variable(typeof(TDestination));
        var allocate = Expression.New(typeof(TDestination));
        var assignTarget = Expression.Assign(target, allocate);

        var statements = new List<Expression>
        {
            assignTarget
        };

        foreach (var sourceProperty in sourceProperties)
        {

            if (targetProperties.TryGetValue(sourceProperty.Name, out PropertyInfo targetProperty))
            {
                Expression sourcePropertyAssign;

                if (sourceProperty.PropertyType != targetProperty.PropertyType &&
                    sourceProperty.PropertyType == typeof(string))
                {
                    var property = Expression.Property(source, sourceProperty);

                    sourcePropertyAssign = Expression.Call(Expression.Convert(property, typeof(object)),
                                                            typeof(object).GetMethod("ToString"));
                }
                else
                {
                    sourcePropertyAssign = Expression.Property(source, sourceProperty);
                }


                var assignProperty = Expression.Assign(
                    Expression.Property(target, targetProperty),
                    sourcePropertyAssign);

                statements.Add(assignProperty);
            }
        }

        statements.Add(target);

        var body = Expression.Block(new[] { target }, statements);

        return Expression.Lambda<Func<TSource, TDestination>>(body, source);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM