1

I am trying to map to a class that contains a property with a private setter. I read in a different answer that this was possible (Automapper apparently used Reflection to do so) just as long as I configured the property in question with the ForMember method (see below)

Mapper config

      var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<IDbCoverage, ICoverage>()
            .ForMember(dest => dest.CoverageCodeDesc, conf => conf.MapFrom(src => src.CoverageCodeDesc)); 
        });

Interfaces

public interface IDbCoverage
{
    string ExternalMemberId { get; set; }
    string CoverageCode { get; set; }
    string CoverageCodeDesc { get; }
}


public interface ICoverage
{
    string ExternalMemberId { get; set; }       
    string CoverageCode { get; set; }
    string CoverageCodeDesc { get; }
}

Instantiation of the destination class:

public class Coverage 
{
    public string ExternalMemberId { get; set; }
    public string CoverageCode { get; set; } 
    public string CoverageCodeDesc { get; private set; }
}

However when I try this only the properties with public setters are mapped. The source value of the other property is not mapped and the destination class ends up with a null value.

What am I doing wrong?

Sperick
  • 2,691
  • 6
  • 30
  • 55
  • I've tried the latest and it just works, you don't need the `MapFrom`. – Lucian Bargaoanu Jul 30 '21 at 16:49
  • @LucianBargaoanu can you explain that a bit more please? If I remove the MapFrom it does not compile. If I remove the entire ForMember method it does not map the property. – Sperick Jul 30 '21 at 16:58
  • A repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail. – Lucian Bargaoanu Jul 30 '21 at 17:19
  • @LucianBargaoanu [here you go](https://gist.github.com/Sperick/9ecb3226ea46abda4cafeb3971c6cfcf) – Sperick Jul 30 '21 at 18:09
  • The interface has no setter. There is no connection between that class and the mapping. Drop the interfaces and create the map for the classes and the problem is solved. – Lucian Bargaoanu Jul 30 '21 at 18:18

0 Answers0