相关疑难解决方法(0)

在c#中存储引用

我正在尝试设计一个类,它可以在销毁时更新对象(类外)的引用.

所以基本上你创建了这个对象的一个​​实例并传递了一个引用类型(以任何方式,构造函数等),然后在对象的销毁时,原始引用已经改变为由对象创建的引用.

如果我通过引用传递引用(比如在构造中),我无法想出一种方法来存储此引用(作为参考)以供析构函数更新它?例如(伪):

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
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的关键是我不是要试图从这个类中改变原来的"外部"对象,我试图"重新命名"它,或者如果你愿意,可以将它初始化.

c# reference-type

6
推荐指数
2
解决办法
7299
查看次数

标签 统计

c# ×1

reference-type ×1