这可能是一个有点奇怪的问题,但是是否有任何可靠的方法来序列化其类扩展 CLR 类型的 IronPython 对象?
例如:
class Foo(System.Collections.Generic.List[str]):
def Test(self):
print "test!"
System.Collections.Generic.List<string>
可以用 Pickle 序列化,因为它实现了 ISerializable
接口(interface),但可序列化 CLR 类型的发出子类似乎不起作用,我得到 ImportError: No module named Generic in mscorlib, Version=4
运行时 pickle.dumps(Foo())
.
此外,运行通常的 Formatter.Serialize(stream, object)
给我:
SystemError: Type 'IronPython.NewTypes.System.Collections.Generic.List`1_4$4' in Assembly Snippets.scripting, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
在嵌入式 C# 环境中运行时,如何实现 IronPython 对象的序列化?
最佳答案
我不知道这是否是你所追求的,但你可以考虑 protobuf ( here ) 的 python 版本?请注意,我还没有专门在 ironpython 上测试过它。这具有额外的优势,即还有 C# 实现可能会有所帮助,同时保持平台独立性。如果可能,我希望让 protobuf-net 支持 DLR 类型,但这是一项艰巨的工作。
作为旁注,我个人建议使用专用的 DTO 类型,而不是尝试扩展内置类型。
关于c# - 序列化从 CLR 类型继承的 IronPython 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3734063/