将一个对象属性值转移到另一个对象属性值

age*_*t47 4 c# mapping reflection.emit ilgenerator system.reflection

最重要的是,我知道AutoMapper,我不想使用它.因为我正在学习C#而我想深入了解它.所以我想尝试自己做这个问题(下面解释).

但是,我正在尝试创建一个属性复制器来处理一种类型属性的值到另一种属性,如果该属性具有相同的名称和类型,并且可以从源中读取并在目标中可写.我正在使用type.GetProperties()方法.采样方法如下:

    static void Transfer(object source, object target) {

        var sourceType = source.GetType();
        var targetType = target.GetType();

        var sourceProps = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        var targetProps = (from t in targetType.GetProperties()
                           where t.CanWrite
                                 && (t.GetSetMethod().Attributes & MethodAttributes.Static) == 0
                           select t).ToList();

        foreach(var prop in sourceProps) {
            var value = prop.GetValue(source, null);
            var tProp = targetProps
                .FirstOrDefault(p => p.Name == prop.Name &&
                    p.PropertyType.IsAssignableFrom(prop.PropertyType));
            if(tProp != null)
                tProp.SetValue(target, value, null);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它的工作原理,但我在所以读一个答案,即采用System.Reflection.EmitILGenerator后期绑定代表更快速,有较高的性能.但没有更多的解释或任何链接.你能帮我理解加速这段代码的方法吗?或者你可以建议我的一些环节Emit,ILGenerator以及后期绑定的代表吗?或者你认为有什么能帮到我?提前致谢.

COMPELETE Q:

我从@ svick的回答中了解并学到很多东西.但是现在,如果我想将它用作开放式通用方法,我该怎么做呢?这样的事情:

public TTarget Transfer<TSource, TTarget>(TSource source) where TTarget : class, new() { } 
Run Code Online (Sandbox Code Playgroud)

或者延期:

public static TTarget Transfer<TSource, TTarget>(this TSource source) where TTarget : class, new() { } 
Run Code Online (Sandbox Code Playgroud)

svi*_*ick 5

可以使用Reflection.Emit来执行此操作,但通常使用Expressions 会更容易,它会为您提供基本相同的性能.请记住,仅当您缓存已编译的代码时才会产生性能优势,例如Dictionary<Tuple<Type, Type>, Action<object, object>>,我在此处没有这样做.

static void Transfer(object source, object target)
{
    var sourceType = source.GetType();
    var targetType = target.GetType();

    var sourceParameter = Expression.Parameter(typeof(object), "source");
    var targetParameter = Expression.Parameter(typeof(object), "target");

    var sourceVariable = Expression.Variable(sourceType, "castedSource");
    var targetVariable = Expression.Variable(targetType, "castedTarget");

    var expressions = new List<Expression>();

    expressions.Add(Expression.Assign(sourceVariable, Expression.Convert(sourceParameter, sourceType)));
    expressions.Add(Expression.Assign(targetVariable, Expression.Convert(targetParameter, targetType)));

    foreach (var property in sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
    {
        if (!property.CanRead)
            continue;

        var targetProperty = targetType.GetProperty(property.Name, BindingFlags.Public | BindingFlags.Instance);
        if (targetProperty != null
                && targetProperty.CanWrite
                && targetProperty.PropertyType.IsAssignableFrom(property.PropertyType))
        {
            expressions.Add(
                Expression.Assign(
                    Expression.Property(targetVariable, targetProperty),
                    Expression.Convert(
                        Expression.Property(sourceVariable, property), targetProperty.PropertyType)));
        }
    }

    var lambda =
        Expression.Lambda<Action<object, object>>(
            Expression.Block(new[] { sourceVariable, targetVariable }, expressions),
            new[] { sourceParameter, targetParameter });

    var del = lambda.Compile();

    del(source, target);
}
Run Code Online (Sandbox Code Playgroud)

如果你有这个,写你的通用方法是简单的:

public TTarget Transfer<TSource, TTarget>(TSource source)
    where TTarget : class, new()
{
    var target = new TTarget();
    Transfer(source, target);
    return target;
} 
Run Code Online (Sandbox Code Playgroud)

使主工作方法也是通用的并创建Action<TSource, TTarget>,甚至让它直接创建对象和使用是有意义的Func<TSource, TTarget>.但是,如果按照我的建议添加缓存,则意味着您必须使用类似的东西,Dictionary<Tuple<Type, Type>, Delegate>并在从缓存中检索委托后将委托转换为正确的类型.


tva*_*son 4

您可能会考虑仅获取与目标匹配的属性(按名称)。这将显着简化您的代码。

foreach (var property in sourceType.GetProperties( BindingFlags.Public | BindingFlags.Instance))
{
     var targetProperty = targetType.GetProperty( property.Name, BindingFlags.Public | BindingFlags.Instance );
     if (targetProperty != null
          && targetProperty.CanWrite
          && targetProperty.PropertyType.IsAssignableFrom(property.PropertyType))
     {
         targetProperty.SetValue( target, property.GetValue(source, null), null );
     }
}
Run Code Online (Sandbox Code Playgroud)