Using ValueInjecter to map between objects with different property names

前端 未结 2 477
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 06:41

How do I map a property from an object to another object with a different property name?

I have a Product class that looks like this:

public         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 07:40

    If you are using ValueInjecter then you would write a ConventionInjection. See the second sample here

        public class PropToTypeProp : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.TargetProp.Name == c.Source.Type.Name + c.TargetProp.Name;
            }
        }
    

    this injection will do from all properties of TSource.* to TTarget.TSource+*, so you do:

    vm.InjectFrom(product);
    

提交回复
热议问题