C#中:有没有什么办法可以很容易地找到/更新所有对对象的引用? [英] C#: Is there any way to easily find/update all references to an object?

查看:158
本文介绍了C#中:有没有什么办法可以很容易地找到/更新所有对对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读罗克福德Lhotka的专家C#2008 Business Objects公司,其中有这样的事,作为一个数据门户,很好地提取其中的数据从何而来。当使用DataPortal.Update(这一点),正如你可能已经猜到坚持'这个'到数据库中,对象是回来了 - 在坚持本的分贝向它提出,如任何更改。时间戳。

I've been reading Rockford Lhotka's "Expert C# 2008 Business Objects", where there is such a thing as a data portal which nicely abstracts where the data comes from. When using the DataPortal.Update(this), which as you might guess persists 'this' to the database, an object is returned - the persisted 'this' with any changes the db made to it, eg. a timestamp.

Lhotka写频繁,很随便,你必须确保更新到新返回的对象旧对象的所有引用。是有道理的,但有一个简单的方法来找到旧对象的所有引用,并改变他们?显然,GC跟踪引用,是有可能打入了?

Lhotka has written often and very casually, that you have to make sure to update all references to the old object to the new returned object. Makes sense, but is there an easy way to find all references to the old object and change them? Obviously the GC tracks references, is it possible to tap into that?

干杯

推荐答案

在GC实际上并不跟踪引用的对象。相反,它计算出哪些对象是可达的全球首发,并在运行时栈的对象,并执行的颜色填充算法的一些变种。

The GC doesn't actually track the references to the objects. Instead, it calculates which objects are reachable starting from global and stack objects at the runtime, and executing some variant of "flood fill" algorithm.

针对您的问题,为什么不只是有代理持有提及的真实的对象?这样,您就需要更新只在一个地方。

Specifically for your problem, why not just have a proxy holding reference to the "real" object? This way you need to update at only one place.

这篇关于C#中:有没有什么办法可以很容易地找到/更新所有对对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆