1

The AutoMapper version # is 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>. BarBase is an abstract class with a generic concrete implementation Bar<T>; The list needs to hold multiple types of 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.

Question: What am I missing? - Is there an out-of-the-box type mapping configuration I can use here without using the converter?

Marx
  • 113
  • 12

1 Answers1

0

The AutoMapper issue log confirms that a converter is necessary for this type of mapping in the current version.

Marx
  • 113
  • 12