AfterMap not working well when mapping enumerations

Skip to first unread message

Ken Baltrinic

Oct 21, 2010, 1:50:45 PM10/21/10
to AutoMapper-users
I am trying to do some post processing on a mapping of enumerations
and it appears that the enumeration that I get from AfterMap is a
LinFu proxy and any time I try to enumerate over it I get a
NullReferenceException. I am guessing that GetEnumerator is returning
null. Here is a quick test to demo the problem:

public class TestType //Note: problem does not appear if target type
if a primitive.
public int current { get; set; }
public int prior { get; set; }

Mapper.CreateMap<int, TestType>()
.ForMember(tt => tt.current, opt => opt.MapFrom(i => i))
.ForMember(tt => tt.prior, opt => opt.Ignore());

Mapper.CreateMap<IEnumerable<int>, IEnumerable<TestType>>()
.AfterMap((i, tt) =>
var prior = tt.First(); //Throws exception here. If I remove the
call to .First()...
foreach (var t in tt) //Then the same exception throws on this
t.prior = prior.current;
prior = t;

public void post_process_enumerable_mapping()
var actual = Mapper.Map<IEnumerable<int>, IEnumerable<TestType>>(
new[] { 1, 2, 3 });
Assert.That(actual, Has.Count.EqualTo(3));

Jimmy Bogard

Nov 3, 2010, 8:30:07 PM11/3/10
OK so this is a bug, but unfortunately I don't think I'll be able to fix it soon.  The problem is that it's picking up the automapping strategy for mapping (because of the CreateMap call), as opposed to the enumerable strategy.  And with the execution API the way it's designed now, it won't be easy to tease apart for a little bit.

However, with the ongoing configuration model re-write, this should be a snap.

You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Djordje Nikolic

Jun 29, 2021, 5:27:42 PM6/29/21
to AutoMapper-users
So 10 years later, this bug still exists.

Jimmy Bogard

Jul 1, 2021, 9:27:29 AM7/1/21
to automapper-users
Yes no one has fixed it and it doesn't affect me so 🤷‍♂️

To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages