Giter Club home page Giter Club logo

disposable_component's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.