[Ironpython-users] Passing an IronPython function to C#

Doug Blank doug.blank at gmail.com
Fri Mar 16 02:22:12 CET 2012


I am attempting to pass an IronPython function into the following C#
functions, which I import into IronPython:

	public static Func<object> getFunc1(params Func<object> [] functions) {
		return functions[0];
	}
	
	public static Func<object> getFunc2(IList<Func<object>> functions) {
		return functions[0];
	}
	
The first works fine:

python>>> Myro.getFunc1(test1)
<System.Func`1[[System.Object, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]] object at
0x000000000000002B [System.Func`1[System.Object]]>
Ok

But the second gives the following error:

python>>> Myro.getFunc2([test1])
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: Unable to cast object of type
'IronPython.Runtime.PythonFunction' to type
'System.Func`1[System.Object]'.

What am I missing in the second case that prevents the cast?

Thanks for any pointers,

-Doug


More information about the Ironpython-users mailing list