5

I have a nested child class with public properties with private setters. In the parent class I am able to use Automapper to map to the child class and the values of the private setters are being set. Everything I've read points to Automapper not supporting this and requiring a custom constructor in the child class to populate private setters.

Is the current version using reflection or something to map the private setters? The setters are definitely private because in the parent class I am unable set Child.Property but in the same place Mapper.Map is able to set the property.

Monstieur
  • 7,992
  • 10
  • 51
  • 77
  • 1
    possible duplicate of: http://stackoverflow.com/questions/8355024/automapper-mapping-properties-with-private-setters – haim770 Jun 04 '13 at 08:04
  • 1
    Duplicate of http://stackoverflow.com/questions/8355024/automapper-mapping-properties-with-private-setters. – L-Four Jun 04 '13 at 08:05
  • Read my question properly. The question you linked states that it's NOT working with private setters and uses constructor parameters as a workaround, but in my case I am able to map private setters directly. – Monstieur Jun 04 '13 at 08:16
  • Solution: http://stackoverflow.com/a/37407912/1360907 – Chris Xue May 24 '16 at 08:18

1 Answers1

4

Automapper uses reflection extensively, and there is no reason not to use it to set private setters.

Actually, until your question I didn't know that it works.

If we look at unit tests we can found the fact that private setters are set automatically. Please see PrivateSetterInParentWorks.

Note, Silverlight doesn't support setting private fields through reflection. So, the feature doesn't work with it.

Community
  • 1
  • 1
mt_serg
  • 7,487
  • 4
  • 29
  • 45