Stworzyłem ListTypeConverter:Jak używać AutoMappera do odwzorowania obiektu z wieloma ogólnymi listami na inny obiekt z odpowiednimi nie ogólnymi listami?
type ListTypeConverter<'source, 'destination>() =
interface ITypeConverter<'source list, Proxies.List> with
member this.Convert(source, destination, context) =
let proxyList = new Proxies.List()
source
|> List.map(fun item -> _mapper.Map<'source, 'destination>(item))
|> List.iter(fun item -> proxyList.addEnd(item) |> ignore)
proxyList
i użytkowania: ForMemberFs by Ian Griffiths
this.CreateMap<SourceItemType list, Proxies.List>().ConvertUsing<ListTypeConverter<SourceItemType, DestItemType>>()
this.CreateMap<SourceType, DestType>().
ForMemberFs((fun d -> d.MyNonGenericList), (fun opts -> opts.MapFrom(fun s -> s.MyGenericList))).
Działa to dobrze, jeśli mam tylko jedną właściwość na moim pierwotnym mapie, który mapuje z 'a list -> Proxy.List
. Ale w momencie, gdy wprowadzam drugie mapowanie z 'b -> Proxy.List
, otrzymuję wyjątek InvalidCastException.
Przedstawiamy drugą mapowanie powoduje wyjątek:
this.CreateMap<SourceItemType list, Proxies.List>().ConvertUsing<ListTypeConverter<SourceItemType, DestItemType>>()
this.CreateMap<SourceItemType2 list, Proxies.List>().ConvertUsing<ListTypeConverter<SourceItemType2, DestItemType2>>()
this.CreateMap<SourceType, DestType>().
ForMemberFs((fun d -> d.MyNonGenericList), (fun opts -> opts.MapFrom(fun s -> s.MyGenericList))).
ForMemberFs((fun d -> d.MyNonGenericList2), (fun opts -> opts.MapFrom(fun s -> s.MyGenericList2))).
Wyjątek:
Unable to cast object of type 'obj' to type 'DestItemType'