I have a problem with Mapster. There is some classes
public class DtoClass
{
public string Name { get; set; }
public ParamValueDto Value { get; set; }
}
public class ParamValueDto
{
public int Value { get; set; }
}
public class TargetClass
{
public string Name { get; set; }
public object Value { get; set; }
public TargetClass(string name, object value)
{
Name = name;
Value = value;
}
}
public class TargetParamValue
{
public int Value { get; set; }
public TargetParamValue(int value)
{
Value = value;
}
}
Config:
config.ForType<DtoClass, TargetClass>()
.MapToConstructor(true)
.ConstructUsing(x => new TargetClass(x.Name, new TargetParamValue(x.Value)));
Everything is fine, the objects are mapped as expected, but if you add one more constructor to the TargetClass
public class TargetClass
{
public string Name { get; set; }
public object Value { get; set; }
public TargetClass(string name, object value)
{
Name = name;
Value = value;
}
public TargetClass(Foo foo) //Where Foo is any class
{
Name = "fsdf";
Value = new ParamValueDto();
}
}
In the final result, Value
will not contain TargetParamValue
as expected, but ParamValueDto
, as if result ConstructUsing
is ignored (but called) or overwritten. The constructor is called correctly - and TargetParamValue
comes to value
, but the final result will have an incorrect param (with ParamValueDto
in Value
).
(New ctor - not called)