遍历并查找复杂对象图中的所有给定类型实例

发布于 2024-12-28 01:38:08 字数 280 浏览 2 评论 0原文

(使用 vb.Net 4.0)假设您有一个对象,其图相当复杂 - 它具有属性、数组和其他集合、具有自己的属性和集合的子类等。我想完全遍历整个对象图并找到所有实例特定类型 T,然后对这些实例执行特定操作。是否有一种万无一失的方法来执行对象图的完整遍历?即使经过反思,这似乎也是一项艰巨的任务,而且很容易出错。

我想知道二进制序列化,因为这似乎以相当健壮的方式克隆一个对象,无论多么复杂。有什么方法可以修改该技术,以便不将其序列化,而是返回对给定类型 T 的所有子对象的引用列表?但这只是纯粹的猜测,我对任何可行的解决方案持开放态度。

(using vb.Net 4.0) Say you have an object whose graph is fairly complex - it has properties, arrays and other collections, subclasses with their own properties and collections, etc. I want to fully traverse the entire object graph and find all instances of a particular type T, to then perform a particular operation on these instances. Is there a bulletproof way to perform a full traversal of the object graph? Even with reflection, this seems a difficult task that is prone to error.

I was wondering about binary serialization, since that seems to clone an object, no matter how complicated, in a fairly robust manner. Is there any way to modify that technique, such that instead of serializing it instead returns a list of references to all sub-objects of given type T? But that is just pure speculation, I'm open to any feasible solution.

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

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

发布评论

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

评论(1

绻影浮沉 2025-01-04 01:38:08

好吧,我想出了一个方法,尽管它可能不是最好的。因为我的对象的图表非常嵌套&复杂,我决定依靠二进制序列化,因为根据我(有限的)经验,它提供了最彻底的&鲁棒的图遍历。缺点是二进制序列化会显着影响性能,但经过基准测试后,它似乎并不是我的特定情况的限制因素。

基本上,我让我的类型 T 实现 ISerialized,然后我可以通过添加到类来处理后序列化:

 Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo,
                           ByVal context As System.Runtime.Serialization.StreamingContext)

另一种方法(不实现 Iserialized)是使用“OnDeserialized”属性创建一个后序列化方法。

无论如何,您可以将信息放入 StreamingContext 对象中,我用它来描述我想要在类型 T 实例上执行的操作。因此,基本上,序列化父对象,它将查找并序列化类型 T 的所有子对象,然后可以对其进行编码以在反序列化时执行任何所需的任务。接下来,您将只处理序列化的对象。

显然,序列化可能会很复杂,并且这种方法并不适用于所有情况。性能方面也相当不优雅。但我需要一个无忧无虑的生活。彻底的对象图遍历所以你就可以了。

Well I figured out a method, though it probably isn't the best. Because my object's graph was pretty nested & complex, I decided to rely on binary serialization, since in my (limited) experience it offers the most thorough & robust graph traversal. The downside is binary serialization can impact performance significantly, but after benchmarking it doesn't appear to be the limiting factor for my particular situation.

Basically, I have my type T implement ISerializable, then I can handle the post-serialization by adding to the class:

 Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo,
                           ByVal context As System.Runtime.Serialization.StreamingContext)

Another way (without implementing Iserializable) is create a post-serialized method with the "OnDeserialized" attribute.

Anyways, you can put info into the streamingContext object, which I used to describe the operation I want performed on the Type T instances. So basically, serialize the parent object, which will find and serialize all sub-objects of type T, which can then be coded to perform any desired task upon deserialization. Going forward, you then deal only with the serialized objects.

Obviously, serialization can have complications, and this method won't work for all situations. Pretty inelegant performance-wise too. But I needed a hassle-free & thorough object graph traversal so there you go.

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