背景:
有2个主要的类,看起来像这样:
public class Source
{
public Dictionary<AttributeType, object> Attributes { get; set; }
}
public class Target
{
public string Title { get; set; }
public string Description { get; set; }
public List<Attribute> Attributes { get; set; }
}
还有子类型/集合/枚举类型:
public class Attribute
{
public string Name { get; set; }
public string Value { get; set; }
}
public enum AttributeType
{
Title,
Description,
SomethingElse,
Foobar
}
目前我的地图长这样:
CreateMap<Source, Target>()
.ForMember(dest => dest.Description, opt => opt.MapAttribute(AttributeType.Description))
.ForMember(dest => dest.Title, opt => opt.MapAttribute(AttributeType.Title));
MapAttribute
方法通过从 Dictionary
中获取项,并使用我提供的 AttributeType
,将其作为 (名称&值) 对象添加到目标集合中(如果键不存在,则使用 try get 返回一个空对象)...
经过所有这些操作,我的目标集合最终变成了这样:
{
title: "Foo",
attributes: [
{ name: "SomethingElse", value: "Bar" },
{ name: "Title", value: "Foo"}
]
}
问题:
我该如何将其余项目映射到目标类,但需要排除特定的键(例如,标题或描述)。例如,在Target.Attributes集合中排除了具有目标中已定义位置的Source.Attribute项目,“剩余”属性仍然进入Target.Attributes。
为了更加清晰(如果我的源代码是这样的):
{ attributes: { title: "Foo", somethingelse: "Bar" } }
这将被映射到这样一个目标:
{ title: "Foo", attributes: [{ name: "SomethingElse", value: "Bar" }] }
我尝试过这个方法,但它无法编译,提示如下:
仅支持在类型的顶级单独成员上为成员提供自定义配置。
CreateMap<KeyValuePair<AttributeType, object>, Attribute>()
.ForSourceMember(x => x.Key == AttributeType.CompanyName, y => y.Ignore())