1

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)

srvr4vr
  • 173
  • 1
  • 9
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a working [mcve], which can be compiled and tested by others. It looks like you have not provided a mapping from `pbc::RepeatedField` to `IReadOnlyCollection`, is it automatically supported by mapster? – Progman Jan 16 '22 at 12:42
  • Yes, protobuff collections are mapped automatically by Mapster. The problem is that it maps them like hell, ignoring the result of the function in ConstructUsing (but it is called). I tried to recreate on a synthetic example - and everything worked as it should, I'll never know what's wrong. – srvr4vr Jan 17 '22 at 07:44
  • I have reproduced the problem. Updated the question – srvr4vr Jan 17 '22 at 08:26
  • If make `Value` property get-only - it's solve problem too, but, actually, I don't have access to this class – srvr4vr Jan 17 '22 at 10:27

0 Answers0