I'm dealing with a really awful set of generated classes which have a ton of properties of type object that contain various types I want to map. The class mappings seem to work however the property references are just copied directly without mapping the referenced objects.
How can I define a map which will map the objects inside the Items property? I have a ton of objects like this so hoping I can define this fairly simply...
Example:
class Program
{
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TerribleType1, TerribleType1Dto>();
cfg.CreateMap<TerribleType2, TerribleType2Dto>();
cfg.CreateMap<TerribleObject, TerribleObjectDto>();
});
var mapper = config.CreateMapper();
var terribleObject = new TerribleObject
{
Items = new object[] { new TerribleType1 { PropA = "Test1" }, new TerribleType2 { PropA = "Test2" } }
};
var terribleObjectDto = mapper.Map<TerribleObjectDto>(terribleObject);
//Want a TerribleType1Dto but instead I get a TerribleType1
Console.WriteLine(terribleObjectDto.Items[0].GetType().Name);
}
}
class TerribleObject
{
// Contains some TerribleType1 and TerribleType2 objects, these don't share a common base.
public object[] Items { get; set; }
}
class TerribleObjectDto
{
//Want this to have some TerribleType1Dto and TerribleType2Dto objects.
public object[] Items { get; set; }
}
public class TerribleType1
{
public string PropA { get; set; }
}
public class TerribleType1Dto
{
public string PropA { get; set; }
}
public class TerribleType2Dto
{
public string PropA { get; set; }
}
public class TerribleType2
{
public string PropA { get; set; }
}
Based on How can I use Automapper to map an object to an unknown destination type? it is possible to get the configured destination type for a mapping when you know the source type at runtime only. With the help of MapFrom()
it is possible to build this ugly mapping for the inner object
type objects:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TerribleType1, TerribleType1Dto>();
cfg.CreateMap<TerribleType2, TerribleType2Dto>();
cfg.CreateMap<TerribleObject, TerribleObjectDto>()
.ForMember(t => t.Items, m => m.MapFrom((source, target, data, context) =>
{
object[] items = source.Items;
object[] targetArray = new object[items.Length];
for (int i = 0; i < items.Length; i++)
{
object fieldEntry = items[i];
Type destinationType = context.Mapper.ConfigurationProvider
.GetAllTypeMaps()
.Single(it => it.SourceType == fieldEntry.GetType())
.DestinationType;
targetArray[i] = context.Mapper.Map(fieldEntry,
fieldEntry.GetType(),
destinationType);
}
return targetArray;
}));
});
This will convert each object
in the array to the configured destination type. When you run your code now:
Console.WriteLine(terribleObjectDto.Items[0].GetType().Name);
Console.WriteLine(terribleObjectDto.Items[1].GetType().Name);
you will get the following output:
TerribleType1Dto
TerribleType2Dto
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.