samunohito / disposable_component

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DisposableComponents

Tools for more convenient use of IDisposable interface.

Feature

Easy to know if Dispose() has been called!

var disposable = new SampleDisposable();

// false
Console.WriteLine(disposable.IsDisposed); 

disposable.Dispose();

// true
Console.WriteLine(disposable.IsDisposed); 

Notification by event is also possible.

var disposable = new SampleDisposable();

disposable.Disposing += (s, e) =>
{
    Console.WriteLine("Disposing!");
};

disposable.Disposed += (s, e) =>
{
    Console.WriteLine("Disposed!");
};

disposable.Dispose();

Just inherit!
This adds the definition of the IDisposable interface and the IsDisposed property,
which can determine if an object has been disposed of.

public class SampleDisposable : DisposableComponent
{
    private readonly IDisposable _something;

    public SampleDisposable()
    {
        _something = new DummyDisposable();
        
        // If registered to this property, it is destroyed at the same time as the call to Dispose().
        // This is due to DisposableCollection (see below)
        Disposable.Add(_something);
    }

    protected override void OnDisposing()
    {
        // When this object is destroyed (when Dispose() is called), additional processing can be performed.
    }
    
    protected override void OnDisposed()
    {
        // When this object is destroyed (when Dispose() is called), additional processing can be performed.
    }
}

A DisposableCollection is also available to dispose of multiple registered IDisposables at once.

var disposableCollection = new DisposableCollection();

disposableCollection.Add(somethingDisposable1);
disposableCollection.Add(somethingDisposable2);
disposableCollection.Add(somethingDisposable3);

// All at once!
disposableCollection.Dispose();

Installation

See NuGet or GitHub for the latest version.

for PackageManager

Install-Package DisposableComponents -Version 1.1.4

for dotnet

dotnet add PROJECT package DisposableComponents --version 1.1.4

Licence

"DisposableComponents" is under MIT license .

Note

This repository was created with the help of the following third-party tools.

About

License:MIT License


Languages

Language:C# 100.0%