如何将 AutoMapper 配置为区分大小写?

dav*_*d.s 5 c# automapper

我预计以下测试会失败,但事实并非如此。如何将 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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用AutoMapper版本 5.1.1 。

she*_*nku 1

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

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

Mapper.Initialize(cfg => {
  cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
  cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});
Run Code Online (Sandbox Code Playgroud)

或者:

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