How to pass a byref value to Delegate.DynamicInvoke?

Answered How to pass a byref value to Delegate.DynamicInvoke?

  • Thursday, September 13, 2012 6:37 AM
     
      Has Code

    I'm trying the following code:

    let orig = ref <| Vector(X = 1, Y = 1)
    native.DynamicInvoke[| box orig |] |> ignore

    But it results in ArgumentException (FSharpRef[Vector] cannot be converted to Vector&)

All Replies

  • Thursday, September 13, 2012 7:12 AM
     
      Has Code

    You should have a look this: Parameters and Arguments

    TL;TR: you can use the "&" Operator (similar to C++) to pass something as a byref

    Just make sure it's mutable - so this should work:

    let mutable orig = Vector(X = 1, Y = 1)
    native.DynamicInvoke[| &(box orig) |] |> ignore
    What I don't get is why you need a byref at all - AFAIK the DynamicInvoke needs just a param-array - can you give a working snippet so I can try?

  • Thursday, September 13, 2012 7:16 AM
     
     Answered Has Code

    I don't think your code works.

    Also, nevermind, I found solution.

    let args = [| box valueTypeValue |]
    native.DynamicInvoke args |> ignore
    assert(not <| valueTypeValue.Equals(args.[0]))

    So if native is of C# type

    delegate void NativeDelegate(ref SomeValueType v)

    and is changing v, then after calling DynamicInvoke corresponding arg in arguments array (this time - args) will be overwritten.


    • Marked As Answer by lostmsu Thursday, September 13, 2012 7:16 AM
    •