要使用 AutoMapper.Collection 包
AutoMapper 在映射接口列表时会失败,因为它无法确定应该实例化哪个接口的实现。要解决这个问题,需要使用 AutoMapper.Collection 包。这个包提供了对映射接口和接口列表的支持。示例代码如下:
// 安装 AutoMapper.Collection 包
// 定义两个接口 public interface IAnimal { } public interface ICat : IAnimal { }
// 定义两个类,实现接口 public class Animal : IAnimal { } public class Cat : ICat { }
// 使用 CreateMap 方法映射接口列表
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap
// 创建接口列表
List
// 映射接口列表
List>(animals);