[Python.NET] RE: example of calling Python from C# with simple object marshalling

Brian Lloyd brian at zope.com
Wed Nov 19 14:50:30 EST 2003

> In case anyone is trying to use the embedding aspect of this fine package
> (well-done, Brian!), here is a quick-and-dirty starting point that shows
> how one might call a Python function with a variable argument list of .NET
> simple value types (e.g. int, double, string) returning an object list,
> from a .NET application. The in and out lists are ordinary simple type
> objects in the NET wold, and ordinary Python objects in the Python world.
> Note that this is not meant to replace anything Brian has in the Runtime
> DLL. It is simply a proof of concept test I did to begin
> discovering what I
> can do in the C# -> Python direction.
> The following trivial python file has an invoke() method which can be
> called from C# in a very uncluttered way...

<snip good stuff>

I think this points out an area still wanting in the embedding
interface (calling into Python and getting back managed objects
rather than PyObject wrappers).

I'm thinking that a good start would be a variation on what your
example does as an overload for the Invoke method of PyObjects:

  object Invoke(...)
  object[] Invoke(...)

I think it would also be a good idea to have a 'ToObject' method on

  object ToObject();

which would:

  - if the Python object represents a managed object, return
    that object

  - if the Python object is a primitive type that is convertible
    to a managed primitive type, return the converted value

  - otherwise throw an exception

Another option could be to have PyObject implement IConvertible,
though that would be more work, and might not buy you anything
over using:

int i = thing.ToObject() as Int32


Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716
Zope Corporation   http://www.zope.com

More information about the PythonDotNet mailing list