Unfortunately I don't think what you want will work.  If it's your .NET class 
you could implement __reduce_ex__ and I think we'd pick it up and use that for 
the serialization.  But if it's some arbitrary framework class then you'll run 
into trouble.  On IronPython 2.0 you could have an extension class w/ the 
__reduce_ex__ function, have an ExtensionTypeAttribute that links the .NET base 
class and the extension class, and then call RuntimeHelpers.RegisterAssembly 
and we'd pick up the extension method.

Also, we do have a feature request for this on CodePlex: 
http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=13401  Voting 
on it would raise its priority and we'd be more likely to do it sooner rather 
than later.

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Turbett
Sent: Wednesday, March 26, 2008 4:56 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Question about serialization

Thanks Michael for the advice. Must appreciated. Unfortunately I hasn't worked 
out yet tho :(

I can use pickle from C# using Evaluate, or from within a Python class, on a 
"pure" Python object. However any attempt at pickling a python object that 
derives from a C# base result in an exception like this:
Cannot convert MyObject(<MyObject object at 0x000000000000002B>) to Int32

Exploring further, I came across Ops.GetDynamicType() and Ops.GetAttrsNames() 
which I thought might be helpful to get the attributes of an object and 
serialize it somehow (manually), but GetAttrNames requires an ICallerContext, 
and I can't see how to get one of those.

It seems that whilst serializing .NET classes is trivial, and serializing 
python classes is trivial, serializing something that touches both is proving 
quite difficult.

Any further suggestions gratefully received!

Thanks, Paul

Michael Foord wrote:

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

http://www.ironpythoninaction.com



.. [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@lists.ironpython.com<mailto:Users@lists.ironpython.com>

http://lists.ironpython.com/listinfo.cgi/users-ironpython.com







_______________________________________________

Users mailing list

Users@lists.ironpython.com<mailto:Users@lists.ironpython.com>

http://lists.ironpython.com/listinfo.cgi/users-ironpython.com





_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to