Possible error in 6.2.2 for mapping null collections

88 views
Skip to first unread message

d...@directfinancialsolutions.com

unread,
Mar 9, 2018, 6:58:11 PM3/9/18
to AutoMapper-users
In the documentation for AutoMapper it says:

When mapping a collection property, if the source value is null AutoMapper will map the destination field to an empty collection rather than setting the destination value to null.

The code below works in 6.2.1, but doesn't in 6.2.2. We get and exception:

AutoMapper.AutoMapperMappingException
  HResult=0x80131500
  Message=Error mapping types.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>
Inner Exception 1:
ArgumentException: Cannot create an instance of interface type System.Collections.Generic.ISet`1[System.String].


Is this a bug that was introduced in 6.2.2, breaking something with null source collections? Or am I missing something?


using System;
using System.Collections.Generic;
namespace AutoMapperTest
{
    public class Source
    {
        public string[] Values { get; set; }
    }
    public class Destination
    {
        public ISet<String> Values { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Source, Destination>();
            });
            AutoMapper.Mapper.AssertConfigurationIsValid();
            Source source = new Source() { Values = null };
            Destination dest = AutoMapper.Mapper.Map<Destination>(source);
        }
    }
}
Reply all
Reply to author
Forward
0 new messages