代码之家  ›  专栏  ›  技术社区  ›  Ritmo2k

自动映射字典到对象配置

  •  1
  • Ritmo2k  · 技术社区  · 6 年前

    我在用 AutoMapper v8.0.0 ,我有一个 Dictionary<string, object> 与合法的c财产名称不对应的钥匙。用例与 MapToAttribute 中描述的功能 Add a MapFromAttribute for when mapping to an external type . 在那个问题中,有人发布了实现该功能的扩展方法的要点。

    在这种情况下,如果不是从一种类型映射到另一种类型,而是直接映射到以下类型,则如何设置映射器配置:

    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 回复  |  直到 6 年前