MadScout
MadScout

Reputation: 25

Private/Internal not Mapping

I have a strange issue that i've never encountered.

I have 2 classes:

Parent Class:

public class ParentClass
{
   private int? _clientType;

   internal int? Client_Type_ 
   {
      get { return _clientType; }
      set {  _clientType = value; }
   }

   public string Company_Name{ get; set; }

   public string Client_Type
   {
      get { return MyDictionary[_clientType.GetValueOrDefault(0)];
   }
}

Child Class:

public class ChildClass : ParentClass
{
   public string Full_Name { get; set; }
}

I am defining the mapping in my Unity IoC container as such:

var config = new MapperConfiguration(cfg =>
{
   cfg.CreateMap<ParentClass, ChildClass>();
}

IMapper mapper = config.CreateMapper();

container.RegisterInstance(mapper);

and in my code I am mapping as such:

var parentClass = GetParentClass();
var mappedClass = _mapper.Map<ChildClass>(parentClass);

The strange thing is... I can check the values of parentClass and all of the values that I have initially passed into my model (parentClass) are there. However, when it maps to the new object (childClass) - Company Name sticks... but _clientType and Client_Type_ are null (which ultimately affects Client_Type).

Any suggestions on what I am doing wrong?

Upvotes: 0

Views: 59

Answers (1)

Kodre
Kodre

Reputation: 181

If you are using AutoMapper, you must set ShouldMapProperty in initializer to appropriate value.

This is done like this:

Mapper.Initialize(cfg =>
{
     cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
     cfg.CreateMap<Source, Destination>();
});

Upvotes: 1

Related Questions