csharp AutoMapper: Update property values without creating a new object

Normally, we use AutoMapper to Map object like this:

1
2
3
4
5
6
7
Mapper.Initialize(
       (cfg) =>
        {
            cfg.CreateMap<TSource, TTarget>();
        }
    );
target = Mapper.Map<TTarget>(source);

however, this will create a new instanse of TTarget, then the reference of target would be changed. The original object of target will not change in this case. The solution is as follows:

1
Mapper.Map(source, target);
记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计