|
////////////////////////////////////////////////////////////////////////////////////
// DisposableCollection.cs
//
// By Scott McMaster (smcmaste@hotmail.com)
// 09/24/2004
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
namespace ScottMcMaster
{
/// <summary>
/// Implement a collection of Disposable objects that is itself
/// disposable, the goal being to provide a convenient programming model
/// for working with multiple disposable objects at the same time.
/// </summary>
public sealed class DisposableCollection : CollectionBase, IDisposable
{
private bool disposed;
public DisposableCollection()
{
disposed = false;
}
~DisposableCollection()
{
Dispose(false);
}
private void ThrowIfDisposed()
{
if( disposed )
{
throw new ObjectDisposedException( this.GetType().Name );
}
}
private void Dispose(bool disposing)
{
lock(this)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
if( disposing )
{
// Dispose managed resources
foreach( IDisposable target in List )
{
// Watch out for somebody adding the DisposableCollection to itself
// lest we overflow the stack.
if( target != this )
{
target.Dispose();
}
}
List.Clear();
}
}
disposed = true;
}
}
#region CollectionBase Implementation
public IDisposable this[ int index ]
{
get
{
ThrowIfDisposed();
return( (IDisposable) List[index] );
}
set
{
ThrowIfDisposed();
List[index] = value;
}
}
public int Add( IDisposable value )
{
ThrowIfDisposed();
return( List.Add( value ) );
}
public int IndexOf( IDisposable value )
{
ThrowIfDisposed();
return( List.IndexOf( value ) );
}
public void Insert( int index, IDisposable value )
{
ThrowIfDisposed();
List.Insert( index, value );
}
public void Remove( IDisposable value )
{
ThrowIfDisposed();
List.Remove( value );
}
public bool Contains( IDisposable value )
{
ThrowIfDisposed();
return( List.Contains( value ) );
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
// Take yourself off the Finalization queue
// to prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have over 10 years of full-lifecycle software development experience on a variety of large projects. I have a B.S. in Math from the University of Nebraska-Lincoln, a Masters in Software Engineering from Seattle University, and a Masters in Computer Science from the University of Maryland. I specialize in building and installing tools, frameworks, and processes that improve developer productivity and product quality. I am currently based in the Seattle area.