C# 使用 BinaryFormatter 深度复制树结构

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

编辑 1

肯特减轻了我的恐惧。然而,我发现了这一规则的一个例外。我为 Node 类编写了一个方法,该方法将向上遍历层次结构,直到到达根 Node 并返回其哈希码。除了一个对象之外,哈希码自始至终都是相同的。简而言之,愤怒。


我即将编写我的第一个(相对)大型 C# 应用程序。然而,我认为我发现了一个我搞砸了的重大错误。

我的应用程序解析 XML 文件并创建对象层次结构,每个对象都继承自 Node 类,并具有子级列表和节点父级引用。

我需要能够复制这个结构。这个概念是初始结构保存默认数据,您可以获取自己的副本并在使用时对其进行修改。所以我使用了通用的DeepClone< T > 扩展方法使用 BinaryFormatter 来完成此操作。

我的问题是,尽管我觉得我已经知道(并且害怕)答案是什么,但这是否仍然会给我带来重新分配所有这些父节点和子节点的引用的问题?


免责声明:当我完成这篇文章时,我意识到我所犯的所有设计错误以及如何避免这些错误,包括这个错误。我要辩解的是,大学的这个学期将是我第一次上数据结构课程。 ;)我完全希望我未能实现的树中的某些重要部分可以帮助解决这个问题。 >_<

EDIT 1

Kent has alleviated my fears. However, I've found one exception to the rule. I wrote a method for the Node class that would traverse up through the hierarchy until it got to the root Node and return its hash code. The hash code's the same all the way down the line except for one single object. In a word, RAGE.


I'm at the end of writing my first (relatively) large C# application. However, I think I've found a major bug that I screwed up on.

My app parses an XML file and creates a hierarchy of objects, each inheriting from a Node class, with Lists of Children and a Node Parent reference.

I needed to be able to copy this structure over. The concept is the initial structure holds the default data, and you can get your own copy and modify it while you use it. So I used a generic DeepClone< T > extension method to do it with BinaryFormatter.

My question, although I feel I already know (and dread) what the answer, is does this sill leave me with the problem of reassigning the references of all those Parent and Child nodes?


Disclaimer: As I finish this, I'm realizing all the design mistakes I made and how they could have been avoided, including this one. In my defense, this semester at university will be the first time I take a data structures class. ;) I fully expect there to be some vital part of a tree that I failed to implement that would help solve this. >_<

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

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

发布评论

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

评论(1

不即不离 2024-09-24 11:11:01

不会。序列化过程会记录对象之间的引用,反序列化过程会恢复这些关系。

编辑:除非我误解了你的问题 - 它并不完全清楚你的意思。一旦客户端代码对克隆的结构进行了更改,您就可以将这些更改合并到主数据结构中(如果您想要这样做)。

您可能想看看 IEditableObject以更正式的方式处理此类事情。您很可能继续使用序列化作为在编辑之前克隆对象的方法,但您的界面将更加标准化。

No. The serialization process records the references between objects and the deserialization process restores those relationships.

EDIT: unless I misunderstood your question - it's not exactly clear what you meant. Once the client code has made changes to the cloned structure, it is up to you to incorporate those changes into the main data structure, if that's what you want to do.

You might want to take a look at IEditableObject for a more formalized way to handling this kind of thing. You may well continue to use serialization as your means of cloning the object prior to edits, but your interface will be more standardized.

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