调试 C# 中的序列化问题
我最近在我正在开发的应用程序中实现了复制和粘贴功能。这几乎符合预期。
我在用户界面中创建了一个新项目,并且可以根据需要多次复制和粘贴它,不会出现任何问题。
但是,当我复制并粘贴由先前的复制和粘贴操作生成的项目时,我收到 SerializationException
。它抱怨某种类型没有被标记为可序列化。
这就是混乱开始的地方。第一个复制和粘贴操作与第二个相同类型的对象进行交互。但第二个会导致异常。
更详细一点,我有一个类 Slide
,这是复制和粘贴操作的目标类。因此,我将该对象的一个实例放置在剪贴板中,然后将其再次粘贴到同一个容器中。这按预期进行。现在我复制插入的对象并尝试粘贴它。这是抛出异常的时候。该异常抱怨类 SlideEditorUi
。 SlideEditorUi
是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,这种情况下的问题是持有另一个对象引用的事件。
BinaryFormatter
跟随底层字段回到对象,然后boom。如果您可以找到可以使用的违规事件(对于“类似字段”的事件):
或者对于显式实现,请将
[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):
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.该错误与数据对象的一部分不包含
SerializedAttribute
。我不知道为什么它不会在序列化时抛出,但会在重新序列化时抛出。也许看到您尝试序列化的 POCO 会很好。我怀疑,您确实错过了属性或字段的该属性。请注意以下内容(来自上面的文档链接),尤其是。 “graph”一词,这意味着父类、聚合和引用的所有类都需要可序列化,以免引发错误:
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: