3
public class Destination
{
    public decimal Obj1 { set; get; }
    public decimal Obj2 { set; get; }
    public int Obj3 { set; get; }
}

public class Source
{
    public decimal Obj1 { set; get; }
    public decimal Obj2 { set; get; }
    public decimal Obj3 { set; get; }
}

how can I map Source class into Destination , but only Decimal types with Automapper ?

Mironline
  • 2,755
  • 7
  • 35
  • 61

1 Answers1

4

I think you can use conditional mapping for this:

The following example will only map properties with source and destination type decimal. You can define your mapping like this:

Mapper.CreateMap<Source, Destination>()
                .ForAllMembers(s=>s.Condition(t =>t.SourceType == typeof(decimal) 
                                               && t.DestinationType == typeof(decimal)));

And then use the mapping like this:

  var src = new Source();
  src.Obj1 = 1;
  src.Obj2 = 2;
  src.Obj3 = 3;
  var dst  = Mapper.Map<Destination>(src);

The dst variable will now have only the Obj1 and Obj2 properties mapped. Obj3 is 0 (default value of an int).

Not sure if this is exactly what you mean. Maybe you only want to check source property type or destination property type, but i hope you get the point.

The above is a generic approach which will still work if more properties / types are getting added to the classes.

Jeroen1984
  • 1,616
  • 1
  • 19
  • 32