我创建了一个ListTypeConverter:
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
Run Code Online (Sandbox Code Playgroud)
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))).
Run Code Online (Sandbox Code Playgroud)
如果我的主地图上只有一个属性从a映射,这样可以正常工作'a list -> Proxy.List.但是当我'b -> Proxy.List从那时引入第二个映射时,我得到一个InvalidCastException.
引入第二个映射会导致异常:
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), …Run Code Online (Sandbox Code Playgroud) 我正在使用F#3.0和.NET 4.5 beta,我正在尝试将F#类型的引用转换Expr<'a -> 'b>为LINQ Expression<Func<'a, 'b>>.
我发现了几个可以解决这个问题的问题,但这些技术似乎不再起作用,可能是由于F#3.0或.NET 4.5的变化.
在这两种情况下,当我从任一问题的解决方案运行代码时,以下操作会引发异常:
mc.Arguments.[0] :?> LambdaExpression
Run Code Online (Sandbox Code Playgroud)
...其中mc一个MethodCallExpression.例外是:
System.InvalidCastException:无法将类型为"System.Linq.Expressions.MethodCallExpressionN"的对象强制转换为"System.Linq.Expressions.LambdaExpression".
不,最后的额外"N" MethodCallExpressionN不是拼写错误.有没有人有建议?谢谢.
UPDATE
这是一个完整的复制品.事实证明这个代码在类似的表达式上运行良好<@ fun x -> x + 1 @>.我的问题是,在我的情况,我需要一个转换Expr<'a -> 'b>成Expr<'a -> obj>这样我就不必垃圾与我所有的lambda表达式box.我通过将原始表达式拼接到这个表达式中来实现:<@ %exp >> box @>.这会生成一个具有正确类型的对象,但转换为Expression<Func<'a, obj>>不再有效的代码.
module Expr =
open System
open System.Linq.Expressions
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.QuotationEvaluation
let rec private translateExpr (linq:Expression) =
match linq with
| …Run Code Online (Sandbox Code Playgroud)