Skip to content

Misleading exception message for ctor argument names #719

Open
@Ekkeir

Description

@Ekkeir

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions