Ivan Ivan - 2 months ago 11
C# Question

Callback from C# to F#

I have an F# class library as such:

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())


Adding a reference of this F# assembly into a C# project, I have tested that I can call LoggerOutput fine from C#. However, if I try to have C# event callback execute in F# I get an error on the line below:

From 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

Answer

you are invoking your method, which would try and use whatever OnQuote returned as the event handler.

OnQuote is the event handler, so try :-

api.MAPI.OnQuoteChange += fsts.OnQuote;
Comments