如何在F#接口中使用属性?

时间:2013-07-12 17:54:05

标签: f#

我想在我的一个接口中将我的一个参数定义为C#out参数。我意识到F#支持byref但是如何将System.Runtime.InteropServices.OutAttribute应用于我的一个接口参数?

C#接口我试图复制

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

1 个答案:

答案 0 :(得分:11)

以下是一个例子:

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