自动映射器字典到对象配置

时间:2018-12-02 22:14:36

标签: c# automapper

我正在使用AutoMapper v8.0.0,我有一个Dictionary<string, object>,其密钥与合法的c#属性名称不对应。用例与Add a MapFromAttribute for when mapping to an external type中所述的MapToAttribute功能的逆向拟合。在该期中,有人发布了实现该功能的扩展方法的要点。

在这种情况下,当您不是从一种类型映射到另一种类型,而是直接映射到该类型时,如何设置映射器配置,如下所示:

Dictionary<string, object> dictionary = new Dictionary<string, object>
{
    { "name with spaces", "foo" },
    { "name-with-hyphens", "foo" },
    { "nameWithDifferentCASE", "foo" },
};
class MyModel
{
    public string NameWithSpaces { get; set; }
    public string NameWithHyphens { get; set; }
    public string NameWithDifferentCase { get; set; }
}
var myModel= mapper.Map<MyModel>(dictionary);

所有配置表达式均基于源和目标类型。

0 个答案:

没有答案
相关问题