1

Closed

Strange behavior when mapping inner collection

description

Hi,
i'm getting a strange behavior when mapping inner collection. What happens is that the inner property isn't correctly filled. But if I map the inner property I get the mapping done. As shown in the sample bellow, the call to the map the collection to the _to variable works, but when I map the _message variable, the _message.To has 0 items.
 
Any thoughts why?
Thanks
 
//Configuration
Mapper.CreateMap<MailMessage, System.Net.Mail.MailMessage>();
 
Mapper.CreateMap<MailAddress, System.Net.Mail.MailAddress>()
.ConstructUsing(x => new System.Net.Mail.MailAddress(x.Address));
 
Mapper.CreateMap<MailAddressCollection, System.Net.Mail.MailAddressCollection>()
.ConvertUsing(new MailAddressCollectionConverter());
 
Mapper.AssertConfigurationIsValid(); // ALL OK :)
 
public void Send(MailMessage message)
{
var _to = Mapper.Map<MailAddressCollection, System.Net.Mail.MailAddressCollection>(message.To); // OK _To.Count = 1 has expected
System.Net.Mail.MailMessage _message = Mapper.Map<MailMessage, System.Net.Mail.MailMessage>(message); // NOT OK _message.To.Count = 0 :(
 
 
 
internal class MailAddressCollectionConverter : ITypeConverter<MailAddressCollection, System.Net.Mail.MailAddressCollection>
{
    #region ITypeConverter<MailAddressCollection,MailAddressCollection> Members
 
    public System.Net.Mail.MailAddressCollection Convert(ResolutionContext context)
    {
        var col = new System.Net.Mail.MailAddressCollection();
 
        if (!context.IsSourceValueNull)
        {
            var source = context.SourceValue as MailAddressCollection;
            if (source != null)
            {
                foreach (var to in source)
                {
                    col.Add(to.Address);
                }
            }
        }
 
        return col;
    }
 
    #endregion
}
Closed Sep 2, 2011 at 11:46 PM by jbogard
All issues moved to GitHub https://github.com/AutoMapper/AutoMapper/issues

comments

icnocop wrote Jul 29, 2011 at 8:00 PM

Can you show us your custom MailMessage class?