简体   繁体   English

AutoMapper是否支持从非通用基类继承的Open Generics?

[英]Does AutoMapper support Open Generics with inheritance from a non-generic base class?

The AutoMapper version # is 7.0.0. AutoMapper的版本号是7.0.0。

Take the following set of classes: 采取以下类集:

public class Person
{
    public string Name { get; set; }
    public List<BarBase> BarList { get; set; }
}

public class PersonModel
{
    public string Name { get; set; }
    public List<BarModelBase> BarList { get; set; }
}

abstract public class BarBase
{
    public int Id { get; set; }
}

public class Bar<T> : BarBase
{
    public T Value { get; set; }
}

abstract public class BarModelBase
{
    public int Id { get; set; }
}

public class BarModel<T> : BarModelBase
{
    public T Value { get; set; }
}

Person has a property BarList of type List<BarBase> . Person有一个属性BarList类型的List<BarBase> BarBase is an abstract class with a generic concrete implementation Bar<T> ; BarBase是具有通用具体实现Bar<T>的抽象类; The list needs to hold multiple types of T . 该列表需要包含多种类型的T

The following two configurations work. 以下两种配置有效。 The sections commented out do not work as explained in the comments. 注释掉的部分不起作用,如注释中所述。

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap(typeof(BarBase), typeof(BarModelBase))
       .Include(typeof(Bar<string>), typeof(BarModel<string>));

    cfg.CreateMap(typeof(Bar<string>), typeof(BarModel<string>));
    cfg.CreateMap(typeof(Person), typeof(PersonModel));
});

var config = new MapperConfiguration(cfg =>
{
    //Fails with: System.NullReferenceException : Object reference not set to an instance of an object.
    //cfg.CreateMap(typeof(BarBase), typeof(BarModelBase)).As(typeof(BarModel<>));

    //Fails with:  Missing map from AutoMapper.UnitTests.OpenGenerics+Bar`1[T] to AutoMapper.UnitTests.OpenGenerics+BarModel`1[T]. Create using Mapper.CreateMap<Bar`1, BarModel`1>.
    //cfg.CreateMap(typeof(BarBase), typeof(BarModelBase))
    //   .Include(typeof(Bar<>), typeof(BarModel<>));

    cfg.CreateMap<BarBase, BarModelBase>().ConvertUsing((source, destination, context) =>
    {
        System.Type sourceType = source.GetType();
        System.Type structType = sourceType.GetGenericArguments()[0];

        return (BarModelBase)context.Mapper.Map(source, sourceType, typeof(BarModel<>).MakeGenericType(structType));
    });


    cfg.CreateMap(typeof(Person), typeof(PersonModel));
    cfg.CreateMap(typeof(Bar<>), typeof(BarModel<>));
});

To verify that either configuration works: 要验证任何一种配置都有效:

Person person = new Person
{
    Name = "Jack",
    BarList = new List<BarBase>
    {
        new Bar<string>{ Id = 1, Value = "One" },
        new Bar<string>{ Id = 2, Value = "Two" }
    }
};

PersonModel personMapped = config.CreateMapper().Map<PersonModel>(person);

Assert.Equal("One", ((BarModel<string>)personMapped.BarList[0]).Value);

Problem: I can't get AutoMapper's Open Generics to work for this scenario ( Generic<T> extends Non-Generic ) without the converter ("ConvertUsing") or by explicitly mapping each one of the closed generic types. 问题:在没有转换器(“ ConvertUsing”)或未显式映射每个封闭的泛型类型的情况下,我无法使AutoMapper的Open Generics用于这种情况( Generic<T>扩展Non-Generic )。

Question: What am I missing? 问题:我想念什么? - Is there an out-of-the-box type mapping configuration I can use here without using the converter? - 是否可以在不使用转换器的情况下使用现成的类型映射配置?

AutoMapper问题日志确认当前版本中这种类型的映射需要转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM