Reputation: 703
I have a typeSupportedCurrencies
that contains a list of objects of type Currency
:
public class SupportedCurrencies
{
public List<Currency> Currencies { get; set; }
}
public class Currency
{
public string Code { get; set; }
public string AmountMin { get; set; }
public string AmountMax { get; set; }
}
And I want to map it to a list of objects of type SupportedCurrency
:
public class SupportedCurrency
{
public string CurrencyCode { get; set; }
public string MinAmount { get; set; }
public string MaxAmount { get; set; }
}
In my mapping profile I have the following mappings:
CreateMap<Response.Currency, SupportedCurrency>()
.ForMember(p => p.MinAmount, q => q.MapFrom(r => r.AmountMin))
.ForMember(p => p.MaxAmount, q => q.MapFrom(r => r.AmountMax))
.ForMember(p => p.CurrencyCode, q => q.MapFrom(r => r.Code));
CreateMap<SupportedCurrencies, IList<SupportedCurrency>>().ConvertUsing(MappingFunction);
And the converter looks like this:
private IList<SupportedCurrency> MappingFunction(SupportedCurrencies arg1, IList<SupportedCurrency> arg2, ResolutionContext arg3)
{
foreach (var item in arg1.Currencies)
{
arg2.Add(arg3.Mapper.Map<SupportedCurrency>(item));
}
return arg2;
}
But when I try to map it:
_mapper.Map<IList<SupportedCurrency>>(obj01.SupportedCurrencies);
I get back an exception saying:
Method 'get_Item' in type 'Proxy_System.Collections.Generic.IList`1 is not implemented.
What am I doing wrong?
Upvotes: 0
Views: 774
Reputation:
static version of CreateMap
is removed: AutoMapper.Mapper does not contain definition for CreateMap
I do not know whether you have choice of upgrading. But with latest AutoMapper
(10.1.1), following code works (you did not give the completed code so I created object of SupportedCurrencies
)
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Currency, SupportedCurrency>()
.ForMember(p => p.MinAmount, q => q.MapFrom(r => r.AmountMin))
.ForMember(p => p.MaxAmount, q => q.MapFrom(r => r.AmountMax))
.ForMember(p => p.CurrencyCode, q => q.MapFrom(r => r.Code));
});
SupportedCurrencies supportedCurrencies = new SupportedCurrencies();
IMapper mapper = config.CreateMapper();
mapper.Map<IList<SupportedCurrency>>(supportedCurrencies);
Upvotes: 1