Created
May 23, 2018 10:42
-
-
Save mrpmorris/5804406dddcd9f015ef05aee8a68e877 to your computer and use it in GitHub Desktop.
ResolveUsing is still executed when Condition fails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using AutoMapper; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace AutoMapperCondition | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Mapper.Initialize(cfg => | |
{ | |
cfg.CreateMap<Source, Dest>() | |
.ForMember(target => target.Codes, config => | |
{ | |
config.Condition(source => !string.IsNullOrWhiteSpace(source.Codes)); | |
config.ResolveUsing(source => source.Codes.Split(',').Select(code => code.Trim())); | |
}); | |
}); | |
Source s = new Source(); | |
Dest d = Mapper.Instance.Map<Dest>(s); | |
// Exected, valid instance of d with codes == null | |
// Actual, null reference exception in ResolveUsing | |
} | |
} | |
public class Source | |
{ | |
public string Codes { get; set; } | |
} | |
public class Dest | |
{ | |
public List<string> Codes { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment