.net MapsterMapper是否可以指定仅Map某些字段?

kqlmhetl  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(129)

下面是示例代码

public People{
   public int Age{get;set;}
   public string Name{get;set;}
   public string Address{get;set;}
}

public PeopleDto{
  public int Age{get;set;}
  public string Name{get;set;}
  public string Address{get;set;}
}

现在,我想MapAgeName字段,而Address字段保留默认值,我想使用如下函数:

var config= TypeAdapterConfig.GlobalSettings.Default.Config.Clone();
config
.ForType<People, PeopleDto>()
.IncludeFields(src=>src.Age,src=>src.Name)//This method does not currently exist, here is just an example
.MaxDepth(2);

可以这样做吗?
我检索了相关信息,但未找到任何可用信息。

dw1jzc5e

dw1jzc5e1#

已经修好了

var config= TypeAdapterConfig.GlobalSettings.Default.Config.Clone();
config
.ForType<People, PeopleDto>()
.IgnoreMember((m, s) => s == MemberSide.Source)
.Map(d => d.Name, s => s.Name)
.Map(d => d.Age, s => s.Age);

相关问题