See

<i><A href="http://msdn.microsoft.com/en-us/library/dd233189(VS.100).aspx">http://msdn.microsoft.com/en-us/library/dd233189(VS.100).aspx<i></A> or <A href="http://stackoverflow.com/questions/904235/how-to-create-net-compatible-events-in-f">http://stackoverflow.com/questions/904235/how-to-create-net-compatible-events-in-f</A> The key is CLIEventAttribute. The MSDN docs are a useful reference for this stuff, check them out.

By on 2/19/2010 12:14 PM ()

<quote>See </i><a href="http://msdn.microsoft.com/en-us/library/dd233189%28VS.100%29.aspx">http://msdn.microsoft.com/en-us/library/dd233189(VS.100).aspx</a><a href="http://msdn.microsoft.com/en-us/library/dd233189%28VS.100%29.aspx"></a><i> or <a href="http://stackoverflow.com/questions/904235/how-to-create-net-compatible-events-in-f">http://stackoverflow.com/questions/904235/how-to-create-net-compatible-events-in-f</a> The key is CLIEventAttribute. The MSDN docs are a useful reference for this stuff, check them out. </quote> I had seen those, but I thought they didn't answer my question. It was just pilot error, though - they had what I needed. Using FSharpHandler<Touch> solves the problem. However, that led me to wonder if there wasn't a nicer way. What about providing an overload that just takes the C# lambda? <code lang=fsharp> namespace com.restphone type Touch = | TouchesBegan | TouchesMoved | TouchesEnded type TouchDelegate = delegate of Touch -> unit type TouchTest() = let e = Event<Touch>() let p = e.Publish [<CLIEvent>] member self.EventForCSharp = e.Publish member self.add_EventForCSharp (fn: TouchDelegate) = p.Add fn.Invoke member self.FireTouchesBegan () = e.Trigger TouchesBegan </code> And then: <code lang=csharp> namespace ConsoleApplication1 { using com.restphone; using System; using Microsoft.FSharp.Core; using Microsoft.FSharp.Control; class Program { static void Main(string[] args) { var t = new TouchTest(); t.EventForCSharp += new FSharpHandler<Touch>((sender, t1) => { System.Console.WriteLine("first handler: {0}", t1); }); t.EventForCSharp += (sender, t1) => { System.Console.WriteLine("second handler: {0}", t1); }; t.FireTouchesBegan(); } } } </code> It looks like the second handler is slightly more expensive (it allocates a delegate, and probably means another function call every time through the loop), but that might be a worthwhile tradeoff. Anything wrong with doing this?

By on 2/19/2010 2:16 PM ()

Using FSharpHandler<Touch> solves the problem.

However, that led me to wonder if there wasn't a nicer way. What about providing an overload that just takes the C# lambda?

I don't understand what you're talking about or asking here. You don't need to mention FSharpHandler, and you don't need to write the add_EventForCSharp method.

It looks like the second handler is slightly more expensive (it allocates a delegate, and probably means another function call every time through the loop), but that might be a worthwhile tradeoff.

Anything wrong with doing this?

And I don't know what you're referring to here, either. Both lines of C# generate the same code (in the latter case, the C# compiler will automatically insert the delegate creation around the anonymous method that represents the lambda).

By on 2/19/2010 2:52 PM ()

I was just confused.

I think my problem was that I saw that Visual Studio gave this as the suggested insertion:

1
2
3
4
5
          t.EventForCSharp += new 

FSharpHandler<int>(t_EventForCSharp);

And my brain grabbed onto the (bogus) assumption that the solution had to look like that.

By on 2/19/2010 3:58 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper