Remote and destructors in C #

I play with .net forwarding functions, and there is something that I cannot find and cannot find the answer on Google, and how deleting objects works.

I am trying to implement some kind of pool of objects with deletion, for this I have a list of static objects, which basically represent an indicator of a row and a logical state.

When I request a new remote object (durring the consturctor), I check the pool for free, mark it both in use and during the destruction of the object. DismisObject just marks it as β€œfree”,

  public class MyRemotableObject : MarshalByRefObject,IDisposable
{

    private AdvancedString obj;
    public MyRemotableObject()
    {
        aso = strCache.GetFreeObject();
    }
    ~MyRemotableObject()
    {
        Destroy();
    }
    public void Dispose()
    {
        Destroy();
    }
    public void SetMessage(string message)
    {
        if (obj== null) { obj= strCache.GetFreeObject(); }
        obj.str= message;
    }
    public string GetMessage()
    {
        return obj.str;          
    }
    void Destroy()
    {
        if (obj!= null)
        {
            obj.DismisObject();
            obj = null;
        }
    }
}

- - 5 , , , ~ MyRemotableObject() Dispose(), . , . - Dispose ( , , , )

.net / ? ( , CG , 4 2 - 2 , - )

+3
2

ITrackingHandler, , , Dispose .

+2

, IDisposable using, .

.

using (var remotableObj = new MyRemotableObject())
{
    // use it
}

.

0

Source: https://habr.com/ru/post/1711899/


All Articles