推断类型的奇怪反射错误

Strange reflection error on inferred type

我尝试通过反射 运行 f# 代码时出现一些奇怪的效果。
给定以下类型

type Box<'a, 'b> = Box of 'a * 'b

和这个函数

//iToS :: Box<'a,'b> -> Box<string,'b>
let iToS (Box (i, v)) = Box ((sprintf "%A" i), v)

我可以轻松正确地运行下面的代码

let r01 = iToS (Box (1, 1))

但是我需要 运行 在我的系统边界的尖锐边缘使用此功能,唯一的方法是回退到反射使用。
所以我创建了这个函数,它应该采用像上面这样的函数和给定类型的记录并应用它。

let convert<'t> (f:Quotations.Expr) (v:'a) : 't =
    let methi e =
        let rec methi' e =
            match e with
                | Call (x, mi, y) -> mi
                | Lambda (_, body) -> methi' body
                | _ -> failwith <| sprintf "not a function %A" e
        methi' e

    let apply f v =
        let m = methi f
        m.Invoke(null, [|box v|])

    apply f v :?> 't

如果我现在 运行 但是像下面这样。

let r10 = (convert<Box<string, int>> <@ iToS @>) (Box (1, 1))

我收到以下错误

System.ArgumentException : Object of type 'Box`2[System.Int32,System.Int32]' cannot be converted to type 'Box`2[System.Object,System.Object]'.
at System.RuntimeType.CheckValue (System.Object value, System.Reflection.Binder binder, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x0007d] in <8cd55ece525b4760b63de40980e005aa>:0
at System.Reflection.MonoMethod.ConvertValues (System.Reflection.Binder binder, System.Object[] args, System.Reflection.ParameterInfo[] pinfo, System.Globalization.CultureInfo culture, System.Reflection.BindingFlags invokeAttr) [0x0007f] in <8cd55ece525b4760b63de40980e005aa>:0
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00014] in <8cd55ece525b4760b63de40980e005aa>:0
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <8cd55ece525b4760b63de40980e005aa>:0
at convert[t] (Microsoft.FSharp.Quotations.FSharpExpr f, Box`2[a,b] v) [0x00006] in <5831a15618eafa12a745038356a13158>:0
at test convert () [0x000e6] in <5831a15618eafa12a745038356a13158>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in <8cd55ece525b4760b63de40980e005aa>:0

谁在尝试将某些东西转换成 Box<obj, obj>,为什么?
感谢任何帮助

PS:一些说明 ;-)
a) 这显然是一个关于在 F#
上下文中使用反射的问题 b) 是的,我知道我真正的问题可以不加思索地解决,我已经这样做了。它轻松地将我的代码大小增加了 40%。
c) 是的,我知道反射很慢。我愿意用速度(我不需要)换取更清晰的代码。

函数 convert 的签名包含显式泛型参数 't,但不包含 'a。它打破了参数 v.

的类型推断

应该是:

let convert<'t, 'a> (f:Quotations.Expr) (v:'a) : 't

但是显式参数很难用。我宁愿在表达式中存储有关转换的类型信息:

let convert (f:Quotations.Expr<'a -> 't>) (v:'a) : 't 

示例(http://ideone.com/peLJAR):

let r10 = (convert <@ iToS @>) (Box (1, 1))
> val r10 : Box<string,int> = Box ("1",1)

let r20 = (convert <@ iToS @>) (Box (1.0, 1.0))
> val r20 : Box<string,float> = Box ("1.0",1.0)

Who is trying to convert something into a Box and why?

由于您使用的是反射,因此您的 MethodInfo 将通用参数读取为 obj 此处

Call (x, mi, y) -> mi

然后你可以像下面那样upcast

let r10 = (convert<Box<string, obj>> <@ iToS @>) (Box((upcast 1 : obj), (upcast 1 : obj)))