I'm using AutoMapper 2.2.1 to map different business objects to view models. Now I'm getting a InvalidCastExceptions
if I try to map objects which have a property of type CustomList
(see code below).
The exception says that CustomList
cannot be casted to IList
. Which is correct because CustomList
implements IReadOnlyList
and not IList
.
So why automapper tries to cast it in this manner and how to fix/workaround this?
I have these types:
public class MyViewModel : SomeModel { //... some addtional stuff ...}
public class SomeModel {
public CustomList DescriptionList { get; internal set; }
}
public class CustomList : ReadOnlyList<SomeOtherModel> {}
public abstract class ReadOnlyList<TModel> : IReadOnlyList<TModel> {}
//map it
//aList is type of SomeModel
var viewList = Mapper.Map<List<MyViewModel>>(aList);