I am developing a .net core application in which I am using Automapper version 10.0.0
I have the following model class in which I have a Country object. I need to resolve it using CustomIvalue Resolver. When I map from model to domain class I need to map country object while in reverse I am setting string property.
My domain class is:
public class MongoIdentityUser
{
public CompanyInfo Company { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Country Country { get; set; }
public string City { get; set; }
public string EncryptedPassword { get; set; }
}
My Model Class:
public class UserEditModel
{
public CompanyInfoModel Company { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string EncryptedPassword { get; set; }
}
Here is an automapper setting:
CreateMap<UserEditModel, MongoIdentityUser>()
.ForMember(dest => dest.Country, opt => opt.MapFrom<CountryToObjectResolver<UserEditModel, MongoIdentityUser>>())
.ReverseMap()
.ForMember(dest => dest.Country, opt => opt.MapFrom(u => u.Country.Code));
CreateMap<UserListModel, User>().ReverseMap();
Here is my Custom CountryToObject Resolver:
public class CountryToObjectResolver<TSource, TDestination> : IValueResolver<TSource, TDestination, Country>
{
private readonly LookupService _lookupService;
public CountryToObjectResolver(LookupService lookupService)
{
_lookupService = lookupService;
}
public Country Resolve(TSource source, TDestination destination, Country destMember, ResolutionContext context)
{
var country = source.GetType().GetProperty("Country").GetValue(source, null);
if (country != null)
return new Country
{
Name = _lookupService.GetCountryByCode(country.ToString()),
Code = country.ToString()
};
return null;
}
}
It's giving me compile-time error which is
I have the same code in my other project and that is working fine
WHats wrong with the code this time?