Kri*_*s-I 5 .net c# automapper
我有两个类,一个是由 Entity Framework 生成的,另一个是我在任何地方都使用的类。
我的课 :
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
EF类:
public class PERSON
{
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当源为PERSONto 时Person,我找到了解决方案,但我没有找到Personto的解决方案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>();
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3091 次 |
| 最近记录: |