我正在尝试设计一个类,它可以在销毁时更新对对象(类外)的引用。
所以本质上你创建了这个对象的一个实例并传递给它一个引用类型(以任何方式,构造函数等),然后在对象销毁时,原始引用已更改为由对象创建的引用。
如果我通过引用传递引用(例如在构造中),我无法找到一种方法来存储这个引用(作为引用)以供析构函数更新它?例如(伪):
class Updater
{
object privateReference;
public Updater(ref object externalReference)
{
privateReference = externalReference; //is privateReference now a new reference to the original object?
}
~Updater()
{
privateReference = new object(); //therefore this isn't 'repointing' the externalReference
}
}
这里的关键是我不想从这个类中改变原始的“外部”对象,我想“重新指向”它,或者如果你愿意的话将它初始化。