序列化从 CLR 类型继承的 IronPython 对象

发布于 2024-09-19 08:39:09 字数 797 浏览 7 评论 0 原文

这可能是一个有点奇怪的问题,但是有没有可靠的方法来序列化其类扩展 CLR 类型的 IronPython 对象?

例如:

class Foo(System.Collections.Generic.List[str]):
    def Test(self):
        print "test!"

System.Collections.Generic.List 可使用 Pickle 进行序列化,因为它实现了 ISerialized 接口,但发出的可序列化 CLR 类型的子类似乎不起作用,并且在运行 pickle.dumps(Foo()) 时出现 ImportError: No module named Generic in mscorlib, Version=4

此外,运行通常的 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 对象的序列化?

This may be a bit of a weird question, but is there any reliable way to serialize IronPython objects whose classes extend CLR types?

For instance:

class Foo(System.Collections.Generic.List[str]):
    def Test(self):
        print "test!"

System.Collections.Generic.List<string> is serializable with Pickle, as it implements the ISerializable interface, but emitted subclasses of serializable CLR types seem to not work, and i get ImportError: No module named Generic in mscorlib, Version=4 when running pickle.dumps(Foo()).

Additionally, running the usual Formatter.Serialize(stream, object) gives me:

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.

How can I implement serialization of IronPython objects when running in an embedded C# environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深府石板幽径 2024-09-26 08:39:09

我不知道这是否是你想要的,但你可以考虑 python 版本的 protobuf (这里)?请注意,我还没有专门在 IronPython 上测试过它。这还有一个额外的优点,即还有可能有所帮助的 C# 实现,同时保持平台独立性。如果可能的话,我希望 protobuf-net 支持 DLR 类型,但这是一项艰巨的任务。

作为旁注,我个人建议使用专用的 DTO 类型,而不是尝试扩展内置类型。

I don't know if it is what you are after, but you could consider the python version of protobuf (here)? I haven't tested it specifically on ironpython, mind. This has the added advantage that there are also C# implementations that may help, while keeping it platform independent. When possible I want to get protobuf-net to support DLR types, but that is a big job.

As a side note, personally I'd recommend having a dedicated DTO type rather than trying to extend the inbuilt types.

自演自醉 2024-09-26 08:39:09

引用自 clrtype元类

IronPython 不支持反射
如今基于 API 或自定义属性
因为 IronPython 不会发出
为每个 Python 定制 CLR 类型
班级。相反,它通常共享一个
跨多个 Python 的单一 CLR 类型
类。例如,所有三个
这些Python类共享一个
底层 CLR 类型。

class shop(object):
  pass 

class cheese_shop(shop):
  def have_cheese(self, cheese_type):
    return False

class argument_clinic(object):
  def is_right_room(self, room=12):
    return "I've told you once"

import clr
print clr.GetClrType(shop).FullName
print clr.GetClrType(cheese_shop).FullName
print clr.GetClrType(argument_clinic).FullName 

尽管cheese_shop继承自
shop 和 argument_clinic 继承自
对象,所有三个类共享
相同的底层 CLR 类型

,但也许您可以通过 序列化代理

Quote from clrtype metaclasses

IronPython doesn’t support Reflection
based APIs or custom attributes today
because IronPython doesn’t emit a
custom CLR types for every Python
class. Instead, it typically shares a
single CLR type across many Python
classes. For example, all three of
these Python classes share a single
underlying CLR type.

class shop(object):
  pass 

class cheese_shop(shop):
  def have_cheese(self, cheese_type):
    return False

class argument_clinic(object):
  def is_right_room(self, room=12):
    return "I've told you once"

import clr
print clr.GetClrType(shop).FullName
print clr.GetClrType(cheese_shop).FullName
print clr.GetClrType(argument_clinic).FullName 

Even though cheese_shop inherits from
shop and argument_clinic inherits from
object, all three classes share the
same underlying CLR type

I haven't tried, but maybe you can solve this issue with manual serialization via serialization surrogates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文