Problems mapping a type that inherits from IEnumerable

1k Views Asked by At

I have a problem mapping a property containing a custom list that inherits from IEnumerable (if i remove that inheritance, this example works). I have simplified the problem into this model:

public interface IMyEnumerable<T> : IEnumerable<T> { }
public class MyIEnumerable<T> : IMyEnumerable<T>
{
    private readonly IEnumerable<T> _items;

    public MyIEnumerable(IEnumerable<T> items)
    {
        _items = items;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class Source
{
    public List<SourceItem> Items { get; set; }
}

public class Destination
{
    public IMyEnumerable<DestinationItem> Items { get; set; }
}

public class SourceItem
{
    public string Name { get; set; }
}

public class DestinationItem
{
    public string Name { get; set; }
}

Then i try to use is this way:

public class MyResolver : ValueResolver<Source, IMyEnumerable<DestinationItem>>
{
    protected override IMyEnumerable<DestinationItem> ResolveCore(Source source)
    {
        var destinationItems = Mapper.Map<List<SourceItem>, IEnumerable<DestinationItem>>(source.Items);
        return new MyIEnumerable<DestinationItem>(destinationItems);
    }
}

// Mappings
Mapper.CreateMap<Source, Destination>()
    .ForMember(x => x.Items, m => m.ResolveUsing<MyResolver>());
Mapper.CreateMap<SourceItem, DestinationItem>();

// Using the mappings
var source = // not really relevant
var destination = Mapper.Map<Destination>(source);

This gives me the following exception (slightly edited for readability):

Mapping types:
MyIEnumerable`1 -> IMyEnumerable`1
MyIEnumerable`1[[DestinationItem]] -> IMyEnumerable`1[[DestinationItem]]

Destination path:
Destination.Items.Items

Source value:
MyIEnumerable`1[DestinationItem]
  ----> System.ArgumentException : Object of type System.Collections.Generic.List`1[DestinationItem] cannot be converted to type IMyEnumerable`1[DestinationItem].

Any idea how i can fix the mapping so that i can get this to work?

1

There are 1 best solutions below

3
On

Assuming the following:

var source = new Source
{
    Items = new List<SourceItem>
    {
        new SourceItem { Name = "foo" },
        new SourceItem { Name = "bar" },
        new SourceItem { Name = "cow" },
    }
};

Then the following work:

// Method 1: Straight up mapping the collections: 
Mapper.CreateMap<List<SourceItem>, IMyEnumerable<DestinationItem>>()
    .ConstructUsing(list => new MyEnumerable<DestinationItem>(list.ConvertAll(Mapper.Map<SourceItem, DestinationItem>)));

// Method 2: Ignore the property and do it ourselves after the rest of the mapping:
Mapper.CreateMap<Source, Destination>()
    .ForMember(q => q.Items, r => r.Ignore())
    .AfterMap((s, d) => d.Items = new MyEnumerable<DestinationItem>(
            s.Items.Select(Mapper.Map<SourceItem, DestinationItem>)));

Nothing else seems to work due to some combination of covariance and contravariance between List<T>, IEnumerable<T> and IMyEnumerable<T>