1
Vote

Mapping from generic interface (IList<>) to generic concrete (List<>)

description

If you try to map from IList<MyType> to List<MyType> an exception is raised:
An unhandled exception of type 'System.InvalidCastException' occurred in EmitMapperAssembly.dll

Additional information: Unable to cast object of type 'TestEmitMapper.FromAnother' to type 'System.Collections.Generic.IList`1[TestEmitMapper.FromAnother]'.
Instead if you try to map for example from IList<string> to List<string> it works fine.

Code to reproduce the problem in a console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmitMapper;

namespace TestEmitMapper
{
    class Program
    {
        static void Main(string[] args)
        {
            var from = new From
            {
                List = new List<FromAnother>
                {
                    new FromAnother { Description = "Hello" },
                    new FromAnother { Description = "World!" }
                }
            };
            var to = ObjectMapperManager.DefaultInstance.GetMapper<From, To>().Map(from);
            
            Console.WriteLine(to.List[0].Description);
            Console.WriteLine(to.List[1].Description);

            Console.ReadLine();
        }
    }

    public class From
    {
        // if you use instead a simple IList<string> it works
        public IList<FromAnother> List { get; set; }
    }

    public class To
    {
        public List<ToAnother> List { get; set; }
    }

    public class FromAnother
    {
        public string Description { get; set; }
    }

    public class ToAnother
    {
        public string Description { get; set; }
    }
}
Any ideas?

comments