Skip to content

Instantly share code, notes, and snippets.

@mrpmorris
Created May 23, 2018 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrpmorris/5804406dddcd9f015ef05aee8a68e877 to your computer and use it in GitHub Desktop.
Save mrpmorris/5804406dddcd9f015ef05aee8a68e877 to your computer and use it in GitHub Desktop.
ResolveUsing is still executed when Condition fails
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