AutoMapper how to map a CamelCase Dictionary<string, object> to a PascalCase C# object

£可爱£侵袭症+ 提交于 2019-12-11 04:25:56

问题


I'm trying to map a Dictionary<string, object> where properties are keyed with Camel casing to a C# object with Pascal casing:

var id = Guid.NewGuid();
var dto = new Dictionary<string, object> {
    { "id", id.ToString() },
    { "value", "some value" },
};

var mapper = new MapperConfiguration(cfg => { }).CreateMapper();
var result = mapper.Map<TestClass>(dto);

var expected = new TestClass {
    Id = id,
    Value = "some value",
};

result.Should().BeEquivalentTo(expected);

If I put the dictionary keys as PascalCase, it works out of the box. I'm guessing I need a custom INamingConvention, but the documentation isn't quite clear about what it should be like...

Can someone help me with that?

using Automapper v7.0.1

Yes, I could do it the long way and map it manually, I know


Edit

There is a closed issue in AutoMapper's github repo: https://github.com/AutoMapper/AutoMapper/issues/2903

来源:https://stackoverflow.com/questions/52430461/automapper-how-to-map-a-camelcase-dictionarystring-object-to-a-pascalcase-c-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!