Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Misleading exception message for ctor argument names

Open Ekkeir opened this issue 8 months ago • 1 comments

There's an exception produced when mapping to a class with a ctor:

public class Destination
{
    public Destination(int number)
    {
        Id = number;
    }

    public int Id { get; }
}

public class Source
{
    public int Number { get; set; }
}

[Fact]
public void Should_Map()
{
    var config = new TypeAdapterConfig();

    config.ForType<Source, Destination>()
        .Map(dest => dest.Id, source => source.Number);

    config.Compile(); // throws an exception
}

The following exception is produced:

System.InvalidOperationException : No default constructor for type 'Destination', please use 'ConstructUsing' or 'MapWith'.

The exception message seems misleading as (I may have missed it in documentation) mapster seems to depend on argument names. Renaming Destination ctor argument from number to id is enough for the test to succeed. That is, changing to the following:

public class Destination
{
    public Destination(int id)
    {
        Id = id;
    }

    public int Id { get; }
}

Suggestions:

  • maybe this name-dependency could be reduced?
  • it would be great if the exception message could mention that there's an option of renaming ctor arguments to match the properties

Ekkeir avatar Jun 21 '24 12:06 Ekkeir