Skip to content

Cannot map properties that have differt names and init access modifier #656

@nicoarm93

Description

@nicoarm93

Hi,
I have a problem with version 7.4. If I want to map two properties with different names and the destination has a different name and the init modifier, Mapster throws a NullReferenceException. This happens when in the configuration of Mapster I set PreserveReference to true.

Here's the code

{
    public Mapper Mapper { get; }

    public MapsterTest()
    {
        var config = new TypeAdapterConfig();
        config.Default.Settings.PreserveReference = true;
        config.Scan(Assembly.GetAssembly(typeof(MapsterTest)));
        config.Compile();
        Mapper = new Mapper(config);
    }
}

internal class MyClass
{
    public int MyProperty { get; set; }
}

internal class DtoClass
{
    public int MyPropertyDto { get; init; }
}
internal class Register : IRegister
{
    void IRegister.Register(TypeAdapterConfig config)
    {
        config.NewConfig<MyClass, DtoClass>()
            .Map(x => x.MyPropertyDto, x=> x.MyProperty);
    }
}

And here's the mapping


var from = new MyClass { MyProperty = 1};

var to = mapster.Mapper.Map<DtoClass>(from);

(The problem does not manifest if the properties have the same name or another modifier (set; or private set;)
This code works fine with version 7.3

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