gpt4 book ai didi

c# - Automapper,INamingConvention Camelcase 属性为带下划线的大写

转载 作者:行者123 更新时间:2023-11-30 13:03:41 24 4
gpt4 key购买 nike

我有两个类,一个由 Entity Framework 生成,另一个是我在任何地方都使用的类。

我的类(class):

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

EF 类:

public class PERSON
{
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
}

当源是 PERSONPerson 时,我找到了解决方案,但是我没有找到 Person< 的解决方案PERSON(属性以大写和下划线分隔)。

The solution for PERSON to Person :

Mapper.Initialize(x => x.AddProfile<Profile1>());
var res = Mapper.Map<PERSON, Person>(person);

public class UpperUnderscoreNamingConvention : INamingConvention
{
private readonly Regex _splittingExpression = new Regex(@"[p{Lu}0-9]+(?=_?)");
public Regex SplittingExpression
{
get { return _splittingExpression; }
}

public string SeparatorCharacter
{
get { return "_"; }
}
}

public class Profile1 : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
CreateMap<PERSON, Person>();
}
}

最佳答案

这适用于 AutoMapper 7.0.1 版:

using AutoMapper;
using System.Text.RegularExpressions;


namespace Data.Service.Mapping
{
public class UpperUnderscoreNamingConvention: INamingConvention
{
public Regex SplittingExpression { get; } = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=_?)");

public string SeparatorCharacter => "_";

public string ReplaceValue(Match match) => match.Value.ToUpper();
}
}

关于c# - Automapper,INamingConvention Camelcase 属性为带下划线的大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12000466/

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