我试图从f#调用ac#函数,其中c#函数将一个函数(委托?)作为参数,我需要这个参数为af #function.例如:
样本c#
public static void function_1(double x, ref double y)
{
y = Math.Exp(x);
}
main()
{
state s;
call_func(s, function_1)
}
Run Code Online (Sandbox Code Playgroud)
所以,call_func有一个类型的参数void fn(double, ref double)
在f#我尝试过:
let function_1 (x:double) (y:double byref) =
let y = 6.0
()
let test =
let s = new state
let ret = call_func(s, function_1)
Run Code Online (Sandbox Code Playgroud)
但是,当它应该是委托的function_1类型double -> double byref -> unit时,我得到f#具有类型的错误void fn(double, ref double).
我可以投射类型或类似的东西吗?还是有错误?
这让我很难过.
我已经阅读了关于这个主题的SO帖子:这里是最相关的,但它没有涵盖将代表传递给一个事件(尽管我预计它会很简单).
具体来说,VS中的错误是:
This function value is being used to construct a delegate type whose signature includes a byref argument. You must use an explicit lambda expression taking 2 arguments.
Run Code Online (Sandbox Code Playgroud)
我能找到的错误的最佳推理是在Eric Lippert的博客中,但我认为我已经在下面处理了它.
// Documentation: http://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx
// Don't forget to unwrap the instance before using it.
let mutable ieInstance : option<InternetExplorer> = None
let onDocumentComplete (pDisp : Object) (url : byref<Object>) =
let doc = ieInstance.Value.Document :?> IHTMLDocument2
let window = doc.parentWindow
window.execScript(@"alert('Message added by addon.');") |> …Run Code Online (Sandbox Code Playgroud)