[IronPython] Return value from Execute()

Jeff Hardy jdhardy at gmail.com
Mon Oct 11 21:09:55 CEST 2010

On Mon, Oct 11, 2010 at 11:40 AM, Mark Senko
<msenko at completegenomics.com> wrote:
> But I’ve had an impossible time trying to figure out how to iterate over an
> IronPython.Runtime.List as returned by a command such as dir() since it
> seems to not be enumerable.
> Typecasting this object doesn’t to an array or a List<> doesn’t seem to
> work.

Have you tried casting to IEnumerable? That has always* worked for me.

    var _pythonRes = source.Execute(_pscope);
    if(_pythonRes is IEnumerable) { ... }

Or, in C# 4, you should be able to make _pythonRes dynamic:
    dynamic _pythonRes = source.Execute(_pscope);
    foreach(dynamic r in _pythonRes) { ... }

Whichever works better for you.

- Jeff

* There used to be some bugs, but they should be long gone.

More information about the Ironpython-users mailing list