如何正确的代码提供给一个ProvidedConstructor的InvokeCode在生成型提供商,其会做以下的相同呢?
鉴于:
module Utils =
let someFun (s : string) (inst : obj) =
// Does something here...
()
Run Code Online (Sandbox Code Playgroud)
我需要有效地生成类型:
type NewGeneratedType () as self =
inherit BaseType ()
do
Utils.someFun "Foo" (box self)
Run Code Online (Sandbox Code Playgroud)
我调用基础构造函数,但不知道如何在实例中正确切片并获取调用的函数:
let ctor = ProvidedConstructor([])
let ci = baseType.GetConstructor(BindingFlags.Public ||| BindingFlags.Instance, null, [| |], null)
ctor.BaseConstructorCall <- fun args -> ci, args
// I do not know how to properly call this to match the constructor above
ctor.InvokeCode <- fun args -> <@@ () @@>
Run Code Online (Sandbox Code Playgroud)
我通过以下方式得到了这个工作:
ctor.InvokeCode <-
fun args ->
let this = Seq.head args
let boxed = Expr.Coerce(this, typeof<obj>)
<@@ Utils.someFun "Foo" %%(boxed) @@>
Run Code Online (Sandbox Code Playgroud)
请注意,尝试将前两行移动到拼接处会导致各种错误,但当我将其拉出时,它工作正常。