在 System.Activities.WorkflowApplication 中有一个委托(delegate)属性:
public Action<WorkflowApplicationCompletedEventArgs> Completed { get; set; }
到目前为止,在我的程序中,我有一个变量是此类的实例
我想定义一个 F# 函数来设置它:
let f (e: WorkflowApplicationCompletedEventArgs) =
// body
myInst.Completed <- f
但这会产生错误:
Error 102 This expression was expected to have type Action but here has type 'a -> unit
如何完成函数“f”以满足编译器的要求?
最佳答案
如果你传递一个匿名函数 fun a -> ...
到期望 System.Action<...>
的方法或构造函数或 System.Func<...>
,则自动转换;在任何其他情况下,您需要像@Funk 指示的那样显式转换它。
let f = System.Action<WorkflowApplicationCompletedEventArgs>(fun e ->
// body
)
myInst.Completed <- f
// Another solution:
let f (e: WorkflowApplicationCompletedEventArgs) =
// body
myInst.Completed <- System.Action<_>(f)
关于F# 定义函数以充当 .net 操作的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42842035/