diff --git a/src/AutoMapper/Mappers/TypeMapObjectMapperRegistry.cs b/src/AutoMapper/Mappers/TypeMapObjectMapperRegistry.cs index 6b10ca999d..9e52aec3cd 100644 --- a/src/AutoMapper/Mappers/TypeMapObjectMapperRegistry.cs +++ b/src/AutoMapper/Mappers/TypeMapObjectMapperRegistry.cs @@ -99,6 +99,8 @@ private void MapPropertyValue(ResolutionContext context, IMappingEngineRunner ma throw new AutoMapperMappingException(errorContext, ex); } + if (result.ShouldIgnore) return; + if (propertyMap.UseDestinationValue) { destinationValue = propertyMap.DestinationProperty.GetValue(mappedObject); diff --git a/src/AutoMapper/ResolutionResult.cs b/src/AutoMapper/ResolutionResult.cs index eee1063f51..41f193461a 100644 --- a/src/AutoMapper/ResolutionResult.cs +++ b/src/AutoMapper/ResolutionResult.cs @@ -34,14 +34,20 @@ private ResolutionResult(object value, ResolutionContext context) public Type Type { get { return _type; } } public Type MemberType { get { return _memberType; } } public ResolutionContext Context { get { return _context; } } - - private Type ResolveType(object value, Type memberType) + + public bool ShouldIgnore { get; set; } + + private Type ResolveType(object value, Type memberType) { if (value == null) return memberType; return value.GetType(); } + public ResolutionResult Ignore() + { + return new ResolutionResult(Context) { ShouldIgnore = true }; + } public ResolutionResult New(object value) { diff --git a/src/UnitTests/CustomMapping.cs b/src/UnitTests/CustomMapping.cs index 3de9207925..93d4868765 100644 --- a/src/UnitTests/CustomMapping.cs +++ b/src/UnitTests/CustomMapping.cs @@ -660,6 +660,48 @@ public void Should_use_the_specified_constructor() } } + + public class When_custom_resolver_requests_property_to_be_ignored : AutoMapperSpecBase + { + private Destination _result = new Destination() { Value = 55 }; + + public class Source + { + public int Value { get; set; } + } + + public class Destination + { + public int Value { get; set; } + } + + public class CustomValueResolver : IValueResolver + { + public ResolutionResult Resolve(ResolutionResult source) + { + return source.Ignore(); + } + } + + protected override void Establish_context() + { + Mapper.CreateMap() + .ForMember(d => d.Value, opt => opt.ResolveUsing().FromMember(src => src.Value)); + } + + protected override void Because_of() + { + _result = Mapper.Map(new Source { Value = 5 }, _result); + } + + [Test] + public void Should_not_overwrite_destination_value() + { + _result.Value.ShouldEqual(55); + } + } + + public class When_specifying_member_and_member_resolver_using_string_property_names : AutoMapperSpecBase { private Destination _result;