
csharp AutoMapper: Update property values without creating a new object
Normally, we use AutoMapper to Map object like this:
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:
Mapper.Map(source, target);