7

I would like to define one of my parameters to be a C# out parameter in one of my interfaces. I realize that F# supports byref but how can I apply the System.Runtime.InteropServices.OutAttribute to one of my interface parameters?

C# Interface I am trying to replicate

public interface IStatisticalTests
{
    void JohansenWrapper(
        double[,] dat,
        double alpha,
        bool doAdfPreTests,
        out double cointStatus,
        out JohansenModelParameters[] johansenModelParameters);
}
4

1 回答 1

12

这是一个例子:

open System
open System.Runtime.InteropServices

[<Interface>]
type IPrimitiveParser =
    //
    abstract TryParseInt32 : str:string * [<Out>] value:byref<int> -> bool

[<EntryPoint>]
let main argv =
    let parser =
        { new IPrimitiveParser with
            member __.TryParseInt32 (str, value) =
                let success, v = System.Int32.TryParse str
                if success then value <- v
                success
        }

    match parser.TryParseInt32 "123" with
    | true, value ->
        printfn "The parsed value is %i." value
    | false, _ ->
        printfn "The string could not be parsed."

    0   // Success

这是您的界面,已翻译:

[<Interface>]
type IStatisticalTests =
    //
    abstract JohansenWrapper :
        dat:float[,] *
        alpha:float *
        doAdfPreTests:bool *
        [<Out>] cointStatus:byref<float> *
        [<Out>] johansenModelParameters:byref<JohansenModelParameters[]>
            -> unit
于 2013-07-12T18:04:50.257 回答