[IronPython] Creating a Generic Delegate

Alex James alex at base4.net
Thu Sep 28 00:37:07 CEST 2006


Sink<T>(T args) is very similar to Predicate<T>

I had no problems getting that going using IronPython... have a look here:
www.base4.net/blog.aspx?ID=118 for an example.

Hope that helps

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of James
Sent: Thursday, September 28, 2006 1:49 AM
To: users at lists.ironpython.com
Subject: [IronPython] Creating a Generic Delegate

I'm working with Leslie Sanford's excellent C# MIDI library and I'd
really like to get things working in IronPython.  Here's the incredibly
simple code (in C#) required to connect to an input device and call a
delegate when a ChannelMessage is received.

InputDevice device = new InputDevice(0);
device.Connect(delegate(ChannelMessage message)
    Console.WriteLine("Command: " + message.Command.ToString());

The InputDevice.Connect method has several overloads, so trying a
straight function doesn't work, as you can see from this output:

>>> from music import *
>>> def fn(msg):
...     print 'Command: ' + msg.Command.ToString()
>>> i = InputDevice(0)
>>> i.Connect(fn)
Traceback (most recent call last):
  File , line 0, in <stdin>##33
TypeError: multiple overloads of Connect could match (InputDevice,
  Connect(InputDevice, Sink[ChannelMessage])
  Connect(InputDevice, Sink[SysExMessage])
  Connect(InputDevice, Sink[SysCommonMessage])
  Connect(InputDevice, Sink[SysRealtimeMessage])
  Connect(InputDevice, Sink[int])
  Connect(InputDevice, Sink[Array[Byte]])

A "Sink" is defined as:
public delegate void Sink<T>(T arg)

So is there any way to make this work?  Thanks for any help you can


users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list