调试 C# 中的序列化问题

发布于 2024-09-11 01:17:50 字数 603 浏览 3 评论 0原文

我最近在我正在开发的应用程序中实现了复制和粘贴功能。这几乎符合预期。
我在用户界面中创建了一个新项目,并且可以根据需要多次复制和粘贴它,不会出现任何问题。
但是,当我复制并粘贴由先前的复制和粘贴操作生成的项目时,我收到 SerializationException。它抱怨某种类型没有被标记为可序列化。

这就是混乱开始的地方。第一个复制和粘贴操作与第二个相同类型的对象进行交互。但第二个会导致异常。

更详细一点,我有一个类 Slide,这是复制和粘贴操作的目标类。因此,我将该对象的一个​​实例放置在剪贴板中,然后将其再次粘贴到同一个容器中。这按预期进行。现在我复制插入的对象并尝试粘贴它。这是抛出异常的时候。该异常抱怨类 SlideEditorUiSlideEditorUi 是一个 UserControl,它与名为 SlideEditor 的类进行交互。它又与 Slide 实例交互。但没有 Slide 实例可以引用任何上述类。
所以我真的很想知道为什么序列化过程要考虑这个类。为什么只有当我复制副本时才会出现这种情况?

I recently implemented a Copy&Paste feature into an application I am working on. This works pretty much as intended.
I create a new item in my user interface and can copy and paste it as often as I want without any issues.
But when I copy&paste an item that was produced by a previous copy&paste action, I get a SerializationException. It complains about a certain type not being marked as serializable.

This is where the confusion starts. The first copy&paste action interacts with the same kind of objects as the second. But the second results in the exception.

To be a little more detailed, I have a class Slide, this is the class that is the target of the copy&paste operation. So, I place an instance of that object in the clipboard and paste it again into the same container. This works out as intended. Now I copy that inserted object and it try to paste it. This is when the exception is thrown. The exception complains about a class SlideEditorUi. SlideEditorUi is a UserControl that interacts with a class called SlideEditor. Which in turn interacts with a Slide instance. But no Slide instance has a reference back to any of the for-mentioned classes.
So I am really wondering why the serialization procedure takes this class into account. And why does it only do that when I copy a copy?

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

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

发布评论

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

评论(2

一片旧的回忆 2024-09-18 01:17:50

通常,这种情况下的问题是持有另一个对象引用的事件。 BinaryFormatter 跟随底层字段回到对象,然后boom

如果您可以找到可以使用的违规事件(对于“类似字段”的事件):

[field:NonSerialized]
public event SomeEventHandler EventName;

或者对于显式实现,请将 [NonSerialized] 添加到支持字段。

或者;使用 BinaryFormatter 之外的其他内容;p Json 或 xml 可以创建可以通过字符串使用的简单格式,或者对于更大/更复杂的对象,还有其他二进制序列化格式。

Usually the problem in this scenario is an event holding a reference to another object. BinaryFormatter follows the underlying field back to the object and boom.

If you can find the offending event you can use (for a "field-like" event):

[field:NonSerialized]
public event SomeEventHandler EventName;

or for an explicit implementation, add [NonSerialized] to the backing field.

Alternatively; use something other than BinaryFormatter ;p Json or xml make simple formats that you can use via a string, or for larger / more complex objects there are other binary serialization formats.

烟燃烟灭 2024-09-18 01:17:50

该错误与数据对象的一部分不包含 SerializedAttribute。我不知道为什么它不会在序列化时抛出,但会在重新序列化时抛出。也许看到您尝试序列化的 POCO 会很好。我怀疑,您确实错过了属性或字段的该属性。

请注意以下内容(来自上面的文档链接),尤其是。 “graph”一词,这意味着父类、聚合和引用的所有类都需要可序列化,以免引发错误:

应用 SerializedAttribute
类型的属性表明
该类型的实例可以是
连载了。共同语言
运行时抛出 SerializationException
如果对象图中有任何类型
被序列化没有
SerializedAttribute 属性
已应用。

The error is about a part of your data object that isn't containing the SerializableAttribute. Why it doesn't throw on serialization, but does on reserializing, I don't know. Perhaps it would be good to see the POCO that you're trying to serialize. What I suspect, is that you indeed miss that attribute on a property or field.

Note the following (from the documentation link above), esp. the "graph" word, which means, all classes, of parents, aggregates and references need to be serializable for the error not to throw:

Apply the SerializableAttribute
attribute to a type to indicate that
instances of this type can be
serialized. The common language
runtime throws SerializationException
if any type in the graph of objects
being serialized does not have the
SerializableAttribute attribute
applied.

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