0

Given the following code:

public class POCO {}
public class TestBase {}
public class TestDerivedA : TestBase { public int X {get;set;} }
public class TestDerivedB : TestBase { public POCO X {get;set;} }

class Program {
  static void Main(string[] args) {
    TestDerivedB b = new TestDerivedA().Adapt<TestDerivedB>();
    Assert.IsNull(b.X); //This fails
    POCO p = 0.Adapt<POCO>();
    Assert.IsNull(p); //I'd like to see this work as well, but the earlier assert is my primary goal.
  }
}

The default behavior in Mapster seems to be to map the X value from an int => a new POCO object.

Is there a way to configure Mapster to instead ignore these fields when mapping from an int to a object? I'd like to do this via a rule of some kind, rather than explicitly configuring a mapping for every single TestDerived class in my application.

I don't mind if ints get mapped to strings or enums and vice versa, but I'd like to avoid creating a full blown object with no meaningful conversion setup to map between the two.

Doing this just for properties in classes that derive from a particular base class is good enough for my purposes, but the more general the better. I'd like to just turn this off across the board.

Mapping the field to NULL instead of just ignoring it might work as well, if that's possible.

I've looked at how I might use the following methods in configuration to accomplish this, but I'm drawing a blank so far. They all seem promising at first but I can't quite figure out how to tie it all together.

  • When
  • IgnoreIf
  • MapWith
  • IgnoreMember
  • ForDestinationType
Matt
  • 1
  • 2

0 Answers0