从C#到F#的回调

Iva*_*van 2 c# f#

我有一个F#类库:

namespace FsharpTestSystem

open System
open System.IO

open TCLoggerEntity
open TCIQuoteEntity

type TestCallback() = 
    member this.LoggerOutput() = Logger.Info("F#")
    member this.OnQuote(q : TCQuote) = Console.WriteLine(q.ToString())
Run Code Online (Sandbox Code Playgroud)

将这个F#程序集的引用添加到C#项目中,我测试过我可以从C#中调用LoggerOutput.但是,如果我尝试在F#中执行C#事件回调,则会在下面的行中出现错误:

来自C#:

using FsharpTestSystem;

public delegate void QuoteChangeEvent(TCQuote q);
event QuoteChangeEvent OnQuoteChange;

TestCallback fsts = new TestCallback();
api.MAPI.OnQuoteChange += fsts.OnQuote(); // this doesn't work

fsts.LoggerOutput(); // This works fine
Run Code Online (Sandbox Code Playgroud)

Kei*_*las 5

你正在调用你的方法,它会尝试使用任何返回的OnQuote作为事件处理程序.

OnQuote是事件处理程序,所以尝试: -

api.MAPI.OnQuoteChange += fsts.OnQuote;
Run Code Online (Sandbox Code Playgroud)