How do I properly supply the code to a ProvidedConstructor
's InvokeCode
in a generative type provider which would do the equivalent of the following?
Given:
module Utils =
let someFun (s : string) (inst : obj) =
// Does something here...
()
I need to have the generated type effectively do:
type NewGeneratedType () as self =
inherit BaseType ()
do
Utils.someFun "Foo" (box self)
I have the call to the base constructor working, but don't know how to properly slice in the instance and get the function called:
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 -> <@@ () @@>
I got this working via:
ctor.InvokeCode <-
fun args ->
let this = Seq.head args
let boxed = Expr.Coerce(this, typeof<obj>)
<@@ Utils.someFun "Foo" %%(boxed) @@>
Note that trying to move the first two lines into the splice caused all sorts of errors, but when I pull it out, it works perfectly.