<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:SimSun;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:"\@SimSun";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";
        color:black;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;
        color:black;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body bgcolor=white lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Unfortunately I don&#8217;t think what you want will work.&nbsp; If it&#8217;s
your .NET class you could implement __reduce_ex__ and I think we&#8217;d pick it up
and use that for the serialization.&nbsp; But if it&#8217;s some arbitrary framework class
then you&#8217;ll run into trouble.&nbsp; 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&#8217;d pick up the extension method.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'>Also, we do have a feature request for this on CodePlex: <a
href="http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=13401">http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=13401</a>&nbsp;
Voting on it would raise its priority and we&#8217;d be more likely to do it sooner
rather than later.<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri","sans-serif";
color:#1F497D'><o:p>&nbsp;</o:p></span></p>

<div>

<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>

<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";
color:windowtext'>From:</span></b><span style='font-size:10.0pt;font-family:
"Tahoma","sans-serif";color:windowtext'> users-bounces@lists.ironpython.com
[mailto:users-bounces@lists.ironpython.com] <b>On Behalf Of </b>Paul Turbett<br>
<b>Sent:</b> Wednesday, March 26, 2008 4:56 AM<br>
<b>To:</b> Discussion of IronPython<br>
<b>Subject:</b> Re: [IronPython] Question about serialization<o:p></o:p></span></p>

</div>

</div>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>Thanks Michael for the advice. Must appreciated.
Unfortunately I hasn't worked out yet tho :(<br>
<br>
I can use pickle from C# using Evaluate, or from within a Python class, on a
&quot;pure&quot; Python object. However any attempt at pickling a python object
that derives from a C# base result in an exception like this:<br>
Cannot convert MyObject(&lt;MyObject object at 0x000000000000002B&gt;) to Int32<br>
<br>
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.<br>
<br>
It seems that whilst serializing .NET classes is trivial, and serializing
python classes is trivial, serializing something that touches both is proving
quite difficult.<br>
<br>
Any further suggestions gratefully received!<br>
<br>
Thanks, Paul<br>
<br>
Michael Foord wrote: <o:p></o:p></p>

<pre>Paul Turbett wrote:<o:p></o:p></pre><pre>&nbsp; <o:p></o:p></pre>

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

<pre><o:p>&nbsp;</o:p></pre><pre>Typically for serializing Python objects you would use pickle [1]. The <o:p></o:p></pre><pre>'dumps' function returns a string (make sure you use the text protocol <o:p></o:p></pre><pre>for IronPython).<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>You could then execute code inside the Python engine to serialize / <o:p></o:p></pre><pre>deserialize objects. I've not tried this for instances that inherit from <o:p></o:p></pre><pre>C# base classes - but it should work fine. :-)<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>Michael Foord<o:p></o:p></pre><pre><a
href="http://www.ironpythoninaction.com">http://www.ironpythoninaction.com</a><o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>.. [1] <a
href="http://docs.python.org/lib/module-pickle.html">http://docs.python.org/lib/module-pickle.html</a><o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>&nbsp; <o:p></o:p></pre>

<blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><pre>I'm using IP 1.1.1 (but will move to 2.0 if that will help). Any <o:p></o:p></pre><pre>pointers &amp; advice greatly appreciated!<o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>Thanks, Paul<o:p></o:p></pre><pre>_______________________________________________<o:p></o:p></pre><pre>Users mailing list<o:p></o:p></pre><pre><a
href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><o:p></o:p></pre><pre><a
href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><o:p></o:p></pre><pre>&nbsp; <o:p></o:p></pre><pre>&nbsp;&nbsp;&nbsp;&nbsp;<o:p></o:p></pre></blockquote>

<pre><o:p>&nbsp;</o:p></pre><pre>_______________________________________________<o:p></o:p></pre><pre>Users mailing list<o:p></o:p></pre><pre><a
href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><o:p></o:p></pre><pre><a
href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><o:p></o:p></pre><pre><o:p>&nbsp;</o:p></pre><pre>&nbsp; <o:p></o:p></pre>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

</body>

</html>