AfterMap not working well when mapping enumerations

1,046 views
Skip to first unread message

Ken Baltrinic

unread,
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
line.
{
t.prior = prior.current;
prior = t;
}
});

[Test]
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

unread,
Nov 3, 2010, 8:30:07 PM11/3/10
to automapp...@googlegroups.com
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 automapp...@googlegroups.com.
To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/automapper-users?hl=en.


Djordje Nikolic

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

Jimmy Bogard

unread,
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 automapper-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/automapper-users/b4e7399e-fc6c-44ca-b60b-0505785dccf1n%40googlegroups.com.

Aleksandar Hajduković

unread,
Jan 24, 2023, 7:45:10 AM1/24/23
to AutoMapper-users
Is it possible to fix this? Need AfterMap to work alse when calling ProjectTo
Reply all
Reply to author
Forward
0 new messages