到 PascalCase C# 对象,automapper"/>

首页 > 解决方案 > AutoMapper 如何映射 CamelCase 字典到 PascalCase C# 对象

问题描述

我正在尝试将Dictionary<string, object>使用 Camel 套管键入的属性映射到具有 Pascal 套管的 C# 对象:

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);

如果我将字典键作为 PascalCase,它可以开箱即用。我猜我需要一个 custom INamingConvention,但文档并不清楚它应该是什么样子......

有人可以帮我吗?

使用 Automapper v7.0.1

是的,我可以做很长的路并手动映射它,我知道


编辑

AutoMapper 的 github 存储库中有一个已关闭的问题:https ://github.com/AutoMapper/AutoMapper/issues/2903

标签: automapper

解决方案


推荐阅读