[英]AutoMapper mapping object types
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.我正在处理一组非常糟糕的生成类,它们具有大量类型为 object 的属性,其中包含我想要 map 的各种类型。 The class mappings seem to work however the property references are just copied directly without mapping the referenced objects.
class 映射似乎可以工作,但是属性引用只是直接复制而不映射引用的对象。
How can I define a map which will map the objects inside the Items property?如何定义 map 它将 map 对象属性内的对象? 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?基于如何使用 Automapper 到 map 和 object 到未知的目的地类型? 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:在
MapFrom()
的帮助下,可以为内部object
类型对象构建这个丑陋的映射:
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.这会将数组中的每个
object
转换为配置的目标类型。 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:您将获得以下 output:
TerribleType1Dto
TerribleType2Dto
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.