[IronPython] Question about serialization

Michael Foord fuzzyman at voidspace.org.uk
Tue Mar 25 14:12:02 CET 2008

Paul Turbett wrote:
> Hi,
> I'm using IP as a scripting engine in a C# app. I have various Python 
> classes that derive from a C# base class, and have there own additional 
> data & functionality.
> I would like to be able to serialize instances of the Python classes 
> from the C# host for persistence across sessions. Using the standard 
> BinaryFormatter with the Serializable attribute is not working - I get 
> an error about the python class not being marked as serializable (the C# 
> base class is marked as serializable though).
> How can I serialize python objects from C#? Should I use reflection to 
> roll my own serializer, or is there something in one of the lesser 
> documented assemblies like Ops I should use?

Typically for serializing Python objects you would use pickle [1]. The 
'dumps' function returns a string (make sure you use the text protocol 
for IronPython).

You could then execute code inside the Python engine to serialize / 
deserialize objects. I've not tried this for instances that inherit from 
C# base classes - but it should work fine. :-)

Michael Foord

.. [1] http://docs.python.org/lib/module-pickle.html

> I'm using IP 1.1.1 (but will move to 2.0 if that will help). Any 
> pointers & advice greatly appreciated!
> Thanks, Paul
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

More information about the Ironpython-users mailing list