Skip to content

Adapt to existing object not work when have readonly property #984

Closed

Description

@ahdung

Please help.

void Main()
{
	var a1=new Foo(){Id=3,Name="aa"};
	var a2=new Foo(){Id=3};
	a1.Adapt(a2);
	a2.Name.Dump(); // will got null
}

class Foo(){
	public int Id {get;set;}
	public string Name{get;set;}
	public string Prop {get;} //when have setter, no problem
}

Activity

  1. DocSvartz commented on Jun 12, 2026

    @DocSvartz
    Contributor

    Hi @ahdung, Do you want to update the property value from readonly property?

  2. ahdung commented on Jun 12, 2026

    @ahdung
    Author

    no, the problem is readonly property cause entire a2 not get updated.

  3. DocSvartz commented on Jun 12, 2026

    @DocSvartz
    Contributor

    What version of Mapster?

  4. ahdung commented on Jun 12, 2026

    @ahdung
    Author

    10.0.7, latest release

  5. DocSvartz commented on Jun 12, 2026

    @DocSvartz
    Contributor

    This is a bug in defining immutable types. Use the latest pre-release version or register the type mapping to itself without any settings.

      TypeAdapterConfig<Foo, Foo>
          .NewConfig();
    
  6. added theissue type on Jun 12, 2026
  7. ahdung commented on Jun 12, 2026

    @ahdung
    Author

    Thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions