У меня есть две модели для моей формы, к которой обращается ViewModel, и из нее выходит ControlModel. ControlModel имеет все те же имена полей и иерархию, но все поля представляют собой строковый тип данных.
Как бы вы закодировали AutoMapper для преобразования строкового поля в целое? Я пробовал Int32.Parse(myString), но Int32 недоступен в выражении (дает ошибку).
Mapper.CreateMap<SourceClass, DestinationClass>()
.ForMember(dest => dest.myInteger,
opt => opt.MapFrom(src => src.myString));
Типы в классе и соответствующие им типы конверсий:
string для int, int?, double, double?, DateTime и bool
Кроме того, есть ли способ обобщить сопоставления таким образом, чтобы все целые числа в цели анализировались с этой функцией? Другими словами, существует ли способ создания сопоставлений для типов данных?
EDIT:
Это выглядит многообещающим:
AutoMapper.Mapper.CreateMap<string, int>()
.ConvertUsing(src => Convert.ToInt32(src));
EDIT: Это сообщение действительно полезно