我正在尝试学习Mapster。我有这样的类
class In
{
public string A;
public string B;
public string C;
}
class Out
{
public Sub Sub;
public string C;
}
class Sub
{
public string A;
public string B;
public Sub(string a, string b)
=>(A,B) = (a,b);
}
创建配置:
var mapper = new Mapper();
mapper.Config
.ForType<In, Out>()
.Map(@out => @out.C, @in=> @in.C)
.Map(@out=> @out.Sub, @in => new Sub(@in.A, @in.B));
现在,如果我尝试映射对象- everythink是ok的,但如果我添加第二个构造函数到Sub
类
class Sub
{
public string A;
public string B;
public Sub(string a, string b)
=>(A,B) = (a,b);
public Sub(string a)=> A=a;
}
Mapster运行时出现异常:
Error while compiling
source=UserQuery+In
destination=UserQuery+Out
type=Map
Error while compiling
source=UserQuery+Sub
destination=UserQuery+Sub
type=Map
No default constructor for type 'Sub', please use 'ConstructUsing' or 'MapWith'
我试着阅读文档,关于ConstructUsing
和MapWith
,这不是我需要的(或者我做错了什么)。我怎么能做到呢?
我发现。只使用
config.ForType<Sub, Sub>().MapToConstructor(true);