[IronPython] Creating a Generic Delegate

James orr.james at gmail.com
Wed Sep 27 15:48:54 CEST 2006


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,
function)
  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
provide.

james




More information about the Ironpython-users mailing list