I'm trying to create an XmlSerializer in an IronPython ASP.NET page to serialize an object with a few properties, but I get this error from the compiler:
Parser Error Message: Unable to generate a temporary class (result=1).
error CS0012: The type 'IronMath.ISlice' is defined in an assembly that is not referenced. You must add a reference to assembly 'IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
I've tried a number of things to get this to work. Added this in my web.config:
<
add
assembly="IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
But to no change. IronMath is in my bin directory. I've also tried to add reference to IronMath in code, but nothing works. This is my code:
a = MyClass(
"Johan",40)
ser = XmlSerializer(a.GetType())
writer = StreamWriter(
"c:/dump.xml")
ser.Serialize(writer,a)
writer.Close()
MyClass looks like this:
class
MyClass:
def __init__(self, name, height):
self.name = name
self.height = height
I guess it's a general problem to serialize objects in Python... Or is there a trick? I could define MyClass and code to serialize in a c# assembly, but I would like to do it in Python only.
To isolate a bit, can you try running the same logic directly from ipy.exe instead of through ASP.NET? If it happens there as well, then at least we know ASP.NET is not in the picture.
Hi, back again... I've installed IronPython 2.0A1 which I believe is based on the Silverlight DLR stuff and done some more testing. Here's the code I'm runnig from the ipy.exe console:
import
clr
import System
clr.AddReference(
"System.Xml")
from
System import *
from
System.IO import *
from
System.Xml.Serialization import *
class Myclass(System.Object):
name = None
age = 0
def __init__(self):
self.name =
""
self.age = 0
def __init__(self, n, a):
self.name = n
self.age = a
def __str__(self):
return "%s, %s years old"%(self.name,self.age)
a = Myclass(
"Angie",66)
print(a)
ser = XmlSerializer(a.GetType())
writer = StreamWriter("C:/dump.xml")
ser.Serialize(writer, a)
writer.Close()
And here's the output from running that piece:
C:\IronPython-2.0A1\IronPython-2.0A1>ipy Program.py
Angie, 66 years old
Traceback (most recent call last):
File Program.py, line 26, in Initialize
File , line 0, in _stub_##31
File , line 0, in NonDefaultNew##36
File , line 0, in .ctor##38
File System.Xml, line unknown, in .ctor
File System.Xml, line unknown, in .ctor
File System.Xml, line unknown, in ImportTypeMapping
File System.Xml, line unknown, in GetTypeModel
File System.Xml, line unknown, in GetTypeDesc
File System.Xml, line unknown, in CheckSupported
SystemError: IronPython.NewTypes.System.Object_1 cannot be serialized because it does not have a parameterless constructor.
Hope this helps. Again, I'm new dynamic languages and not sure how well serialization is supposed to work here. I'm also unfamilar to how constructor overloading works in python I'm afraid, as it might have something to do with the error in this case.
jdanforth
Member
486 Points
16 Posts
Serialize complex objects in IronPython for ASP.NET?
Mar 10, 2007 06:00 PM|LINK
I'm trying to create an XmlSerializer in an IronPython ASP.NET page to serialize an object with a few properties, but I get this error from the compiler:
Parser Error Message: Unable to generate a temporary class (result=1).
error CS0012: The type 'IronMath.ISlice' is defined in an assembly that is not referenced. You must add a reference to assembly 'IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
I've tried a number of things to get this to work. Added this in my web.config:
<
add assembly="IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> But to no change. IronMath is in my bin directory. I've also tried to add reference to IronMath in code, but nothing works. This is my code:a = MyClass(
"Johan",40)ser = XmlSerializer(a.GetType())
writer = StreamWriter(
"c:/dump.xml")ser.Serialize(writer,a)
writer.Close()
MyClass looks like this:
class
MyClass: def __init__(self, name, height):self.name = name
self.height = height
I guess it's a general problem to serialize objects in Python... Or is there a trick? I could define MyClass and code to serialize in a c# assembly, but I would like to do it in Python only.davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Serialize complex objects in IronPython for ASP.NET?
Mar 12, 2007 01:57 AM|LINK
Hi Johan,
To isolate a bit, can you try running the same logic directly from ipy.exe instead of through ASP.NET? If it happens there as well, then at least we know ASP.NET is not in the picture.
thanks,
David
jdanforth
Member
486 Points
16 Posts
Re: Serialize complex objects in IronPython for ASP.NET?
Jul 12, 2007 09:35 AM|LINK
Hi, back again... I've installed IronPython 2.0A1 which I believe is based on the Silverlight DLR stuff and done some more testing. Here's the code I'm runnig from the ipy.exe console:
import
clr import Systemclr.AddReference(
"System.Xml")from
System import *from
System.IO import *from
System.Xml.Serialization import * class Myclass(System.Object): def __init__(self): ""self.age = 0
def __init__(self, n, a): def __str__(self): return "%s, %s years old"%(self.name,self.age)a = Myclass(
"Angie",66) print(a)ser = XmlSerializer(a.GetType())
writer = StreamWriter("C:/dump.xml")ser.Serialize(writer, a)
writer.Close()
And here's the output from running that piece:C:\IronPython-2.0A1\IronPython-2.0A1>ipy Program.py
Angie, 66 years old
Traceback (most recent call last):
File Program.py, line 26, in Initialize
File , line 0, in _stub_##31
File , line 0, in NonDefaultNew##36
File , line 0, in .ctor##38
File System.Xml, line unknown, in .ctor
File System.Xml, line unknown, in .ctor
File System.Xml, line unknown, in ImportTypeMapping
File System.Xml, line unknown, in GetTypeModel
File System.Xml, line unknown, in GetTypeDesc
File System.Xml, line unknown, in CheckSupported
SystemError: IronPython.NewTypes.System.Object_1 cannot be serialized because it does not have a parameterless constructor.
Hope this helps. Again, I'm new dynamic languages and not sure how well serialization is supposed to work here. I'm also unfamilar to how constructor overloading works in python I'm afraid, as it might have something to do with the error in this case.
Thanks,
Johan
IronPython
JoshuaP_Ohio
Member
2 Points
10 Posts
Re: Serialize complex objects in IronPython for ASP.NET?
Mar 20, 2009 07:34 PM|LINK
Hello Johan,
What is the target of your serialized object? Depending on the intended usage, there are other serialization types available:
Does this help?
Cheers,
-JK