为什么 AutoMapper 在映射此 getter-only 属性 时抛出 InvalidCastException?

Why does AutoMapper throw an InvalidCastException when mapping this getter-only property?

根据我的阅读,AutoMapper 应该忽略 getter-only 属性。但是由于这个 getter-only 属性

,映射器抛出一个 InvalidCastException
using AutoMapper;

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Model1, Model2>();
});

var mapper = new Mapper(config);

var m1 = new Model1 
{
    StringList = new List<string> { "String" }
};

var m2 = mapper.Map<Model2>(m1);


public class Model1
{
    public List<string> StringList { get; set; }

    public IEnumerable<object> Objects => StringList;
}

public class Model2
{
    public List<string> StringList { get; set; }

    public IEnumerable<object> Objects => StringList;
}

Map() 行抛出这个异常

AutoMapper.AutoMapperMappingException: 'Error mapping types.'

Inner Exception

InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.Collections.Generic.ICollection`1[System.Object]'.

这不适用于集合,因为它们不需要映射 setter,您可以简单地将 映射到 该集合。

https://docs.automapper.org/en/latest/10.0-Upgrade-Guide.html#all-collections-are-mapped-by-default-even-if-they-have-no-setter