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

Doug Blank doug.blank at gmail.com
Fri Mar 16 12:24:46 CET 2012


On Thu, Mar 15, 2012 at 9:22 PM, Doug Blank <doug.blank at gmail.com> wrote:
> 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,

This is looking more and more like a bug. I see from
http://stackoverflow.com/questions/799987/how-to-pass-a-lambda-expression-to-a-c-sharp-constructor-from-an-ironpython-scri

that it works for constructors (and have verified that it does), but I
can't figure out any way to make it work with a static method. Why the
difference?

If it is a bug, is there a workaround? How can I pass any Python
callable to a C# static method?

Any suggestions appreciated!

-Doug

> -Doug


More information about the Ironpython-users mailing list