3

We've run into an issue mapping a class to an interface using Mapster 7.2.0. Mapping to target interface

public interface ITarget 
{
  int GetOnlyProperty {get;}
  int GetSetProperty {get;set;}
}

with source.Adapt<ITarget>() always results in GetOnlyProperty having the default value 0.

However, if the target interface has all read-only properties, e.g.

public interface IReadonlyTarget 
    {
      int GetOnlyProperty {get;}
      int GetSetProperty {get;}
    }

both GetOnlyProperty and GetSetProperty are set to the right values from the source.

Is this a bug or a feature, and if a feature, how can one make Mapster populate a target interface with both readonly and writeable target properties?

AunAun
  • 1,423
  • 2
  • 14
  • 25
  • Turns out this behavior is by design. An interface with all readonly props is considered "readonly" and mapped via constructor taking all values of all props. Props' backing fields are set in the constructor. Otherwise, mapping is done via default constructor, and readonly props retain their default values. – AunAun Sep 17 '21 at 09:45

0 Answers0