Pass Objects to AutoMapper Mapping

后端 未结 5 832
囚心锁ツ
囚心锁ツ 2020-12-04 18:03

I am working with AutoMapper and some of the values for the entity being mapped to are variables in my current method. I have tried to Google it but to no avail. Can I pass

5条回答
  •  难免孤独
    2020-12-04 18:15

    AutoMapper handles this key-value pair scenario out of the box.

    Mapper.CreateMap()
        .ForMember(d => d.Foo, opt => opt.ResolveUsing(res => res.Context.Options.Items["Foo"]));
    

    Then at runtime:

    Mapper.Map(src, opt => opt.Items["Foo"] = "Bar");
    

    A bit verbose to dig into the context items but there you go.

提交回复
热议问题