Код: Выделить всё
using AutoMapper;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => int.Parse(src.Id)))
.ForMember(dest => dest.InStore, opt => opt.MapFrom(src => src.InStore == "1"));
});
var mapper = config.CreateMapper();
var source = new Source { Id = "123", InStore = "1" };
var destination = mapper.Map(source);
Console.WriteLine($"Id: {destination.Id}, InStore: {destination.InStore}");
public class Source
{
public string Id { get; set; }
public string InStore { get; set; }
}
public class Destination
{
public int Id { get; private set; }
public bool InStore { get; private set; }
public Destination(int id, bool inStore)
{
Id = id;
InStore = inStore;
}
}
Код: Выделить всё
AutoMapper.AutoMapperMappingException: 'Error mapping types.'
FormatException: String '1' was not recognized as a valid Boolean.
Дерево выражений выглядит следующим образом:
Код: Выделить всё
(Source src, Destination dest, ResolutionContext ctxt) => {
Destination typeMapDestination;
return src == default(Source) ? default(Destination) : (
typeMapDestination = dest ?? new Destination((
string resolvedValue,
resolvedValue = false || src == null ? default(string) : src.Id,
Convert.ToInt32(resolvedValue)
), (
string resolvedValue,
resolvedValue = false || src == null ? default(string) : src.InStore,
Convert.ToBoolean(resolvedValue)
)),
if (dest != null) {
try {
(
string resolvedValue,
int propertyValue,
resolvedValue = false || src == null ? default(string) : src.Id,
propertyValue = Convert.ToInt32(resolvedValue),
typeMapDestination.Id = propertyValue
)
} catch (Exception ex) {
throw new AutoMapperMappingException("Error mapping types.", ex, #TypePair, #TypeMap, #PropertyMap);
}
},
if (dest != null) {
try {
(
string resolvedValue,
bool propertyValue,
resolvedValue = false || src == null ? default(string) : src.InStore,
propertyValue = Convert.ToBoolean(resolvedValue),
typeMapDestination.InStore = propertyValue
)
} catch (Exception ex) {
throw new AutoMapperMappingException("Error mapping types.", ex, #TypePair, #TypeMap, #PropertyMap);
}
},
try {
(
string resolvedValue,
resolvedValue = try {
false || src == null ? default(string) : src.Id;
} catch (NullReferenceException) {
default(string);
} catch (ArgumentNullException) {
default(string);
},
typeMapDestination.Name = resolvedValue
)
} catch (Exception ex) {
throw new AutoMapperMappingException("Error mapping types.", ex, #TypePair, #TypeMap, #PropertyMap);
},
typeMapDestination
);
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... lass-has-a