Context:
There are 2 main classes that look something like this:
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; } }
And sub / collection / Enum type:
public class Attribute { public string Name { get; set; } public string Value { get; set; } } public enum AttributeType { Title, Description, SomethingElse, Foobar }
Currently, my map is as follows:
CreateMap<Source, Target>() .ForMember(dest => dest.Description, opt => opt.MapAttribute(AttributeType.Description)) .ForMember(dest => dest.Title, opt => opt.MapAttribute(AttributeType.Title));
Where MapAttribute gets the element from the Dictionary and uses the AttributeType that I provided, adds it to the target collection as an object (name and value) (using try get and returning empty if the key does not exist) ...
After that, all my targets look like this:
{ title: "Foo", attributes: [ { name: "SomethingElse", value: "Bar" }, { name: "Title", value: "Foo"} ] }
Question:How can I move on to displaying the remaining elements in the target class, but I need to be able to exclude certain keys (for example, name or description). EG. Source.Attribute elements that have a specific place in the target are excluded from the Target.Attributes collection, and the "left-over" properties still apply to Target.Attributes.
For clarity (if my source looks like this):
{ attributes: { title: "Foo", somethingelse: "Bar" } }
it will be matched with this type:
{ title: "Foo", attributes: [{ name: "SomethingElse", value: "Bar" }] }
I tried to do this, but it does not compile, stating the following:
Custom configuration for members is only supported for individual top-level members for a type.
CreateMap<KeyValuePair<AttributeType, object>, Attribute>() .ForSourceMember(x => x.Key == AttributeType.CompanyName, y => y.Ignore())