Reputation: 763
I'd like to be able to pass multiple objects to Automapper via the Options dictionary, but the dictionary itself is read only.
E.g. the examples I've found show adding one item like this:
Mapper.Map<Source, Dest>(src, opt => opt.Items["Foo"] = "Bar");
But I'd like to do something more like this:
var mappingOptions = new Dictionary<string, object>();
mappingOptions["foo"] = "foo";
mappingOptions["bar"] = "bar";
var model = _mapper.Map<ThingModel>(realthing,
opt => opt.Items = mappingOptions // readonly, can't be assigned
);
Is adding more than one item possible, maybe just inside the LINQ?
Upvotes: 2
Views: 2851
Reputation: 1871
In such case you have to clear dictionary and populate it with items from source dictionary:
var model = _mapper.Map<ThingModel>(
//realthing,
opt =>
{
opt.Items.Clear();
mappingOptions.Aggregate(
opt.Items,
(items, option) =>
{
items.Add(option.Key, option.Value);
return items;
}
);
}
);
Upvotes: 0
Reputation: 73253
Do you mean like this?
Mapper.Map<Source, Dest>(src, opt =>
{
opt.Items["foo"] = "foo";
opt.Items["bar"] = "bar";
});
Upvotes: 6