开发者

Storing a reference in c#

I'm trying to design a class which can update a reference to an object (outside the class) on destruction.

So essentially you create an instance of this object and pass it a reference type (in whichever manner, constructor etc.) and then on destruction of the object the original reference has changed to a reference created by the object.

If I pass a reference by reference (say in construction) I can't figure a way to store this reference (as a reference) for the destructor to update it? For example (pseudo):

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
    }
}

The key here is I'm not trying to mutate the original 'external' object from this class I'm trying to 'repoint' it, or i开发者_如何学运维nitialise it if you will.


You can't do this, basically. ref is only applicable within the method itself, effectively.

It's not at all clear what you want to use this for - could you give us more information so we can suggest alternative designs? Anything which relies on a finalizer is troubling to start with, to be honest...


This won't work because the ref aspect of the constructor parameter only applies inside the constructor.

What I would do is to give the constructor a delegate that can be used to update the thing in question. Also, you should use IDisposable for this (in conjunction with a using block) rather than a finalizer; finalizers shouldn't touch managed objects (they're designed to free unmanaged objects).

class Updater : IDisposable
{
    Action<object> setter;

    public Updater(Action<object> setter)
    {
        this.setter = setter;
    }

    public Dispose()
    {
        setter(new object());
    }
}


Be aware that using a Finalizer like this isn't the same as a destructor in C++.

When the last reference dies (perhaps it it goes out of scope) the object goes onto the Finalizer queue. The GC determines when to call the Finalizer and it could be a long time after the last reference dies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜