gpt4 book ai didi

c# - 如何配置 AutoMapper 区分大小写?

转载 作者:太空宇宙 更新时间:2023-11-03 15:12:59 26 4
gpt4 key购买 nike

我预计以下测试会失败,但事实并非如此。如何将 AutoMapper 配置为区分大小写?

public class AutomapperTests
{
[Fact]
public void CaseSensitiveTest()
{
Mapper.Initialize(cfg => cfg.AddMemberConfiguration().AddName<CaseSensitiveName>());

Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());

Mapper.AssertConfigurationIsValid();
}

public class Source
{
public int Foo { get; set; }
}

public class Destination
{
public int FoO { get; set; }
}
}

我使用的是 AutoMapper 的 5.1.1 版.

最佳答案

查看命名约定配置:https://github.com/AutoMapper/AutoMapper/wiki/Configuration#naming-conventions

在配置文件或映射器级别,您可以指定源和目标命名约定:

Mapper.Initialize(cfg => {
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});

或者:

public class OrganizationProfile : Profile 
{
public OrganizationProfile()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
//Put your CreateMap... Etc.. here
}
}

关于c# - 如何配置 AutoMapper 区分大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40317324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com