copy chosen properties to object of other type

梦想与她 提交于 2021-02-05 10:38:50

问题


I need to copy properties of one object to another, both objects can be of different type, can have properties of same name. These property can also be of complex type.

I was able to achieve the copy feature for simple TYPE properties, how ever i am unable to achieve this for complex types.. like see below sample snippet

[TestClass]
public class PropAssignTest
{
    [TestMethod]
    public void Test()
    {
        Test1 t1 = new Test1() { Prop1 = "test", TestName = new Test3 { Name = "santosh" } } ;
        Test2 t2 = new Test2();
        Assign<Test1, Test2>(t1, t2, e => e.Prop1);
        Assign<Test1, Test2>(t1, t2, e => e.TestName.Name);//this doesnot work !!
    }

    private void Assign<T1,T2>(T1 T1Obj, T2 T2Obj, Expression<Func<T1, object>> memberLamda)
    {
        var memberSelectorExpression = memberLamda.Body as MemberExpression;
        if (memberSelectorExpression != null)
        {
            var property = memberSelectorExpression.Member as PropertyInfo;
            if (property != null)
            {
                T2Obj.GetType().GetProperty(property.Name).SetValue(T2Obj, property.GetValue(T1Obj));
            }
        }
    }

In above code i want to copy e.TestName.Name, where TestName is a complex type object, wherein i only need to copy Name property of TestName, TestName can however define many properties.

Any suggestions...

Thanks


回答1:


Use AutoMapper and ignore the members you don't want to map.

config.CreateMap<Person, Employee>()
    .ForMember(d => d.Name, o => o.MapFrom(s => s.FullName))
    .ForMember(d => d.Age, o => o.Ignore());



回答2:


You can try using the AutoMapper as @Kevin mentioned. If you still need a custom method, you can try this. Note: It copies the value to the property in the target object itself and does not look for the property as in the expression tree.

private static void Assign<T1, T2>(T1 T1Obj, T2 T2Obj, Expression<Func<T1, object>> memberLamda)
{
    var memberSelectorExpression = memberLamda.Body as MemberExpression;

    if (memberSelectorExpression != null)
    {
        var sourceProperty = memberSelectorExpression.Member as PropertyInfo;

        if (sourceProperty != null)
        {
            var targetProperty = T2Obj.GetType().GetProperty(sourceProperty.Name);

            if (targetProperty != null)
            {
                targetProperty.SetValue(T2Obj, GetValue(T1Obj, memberSelectorExpression));
            }
        }
    }
}

public static object GetValue<T>(T source, MemberExpression expr)
{
    var sourceProperty = expr.Member as PropertyInfo;

    var nextExpression = expr.Expression as MemberExpression;

    if (nextExpression == null)
    {
        return sourceProperty.GetValue(source);
    }

    var sourcePart = GetValue(source, nextExpression);
    return sourceProperty.GetValue(sourcePart);
}


来源:https://stackoverflow.com/questions/44175792/copy-chosen-properties-to-object-of-other-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!