/* Copyright 2010-2014 MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace MongoDB.Shared { /// /// Represents a class derived from an IDisposable class and that itself owns one ore more disposable resources. /// public class CanonicalDisposableDerivedClass : CanonicalDisposableClass { // private fields private IDisposable _anotherDisposableResource; /// /// Initializes a new instance of the class. /// /// A disposable resource. /// Another disposable resource. public CanonicalDisposableDerivedClass(IDisposable disposableResource, IDisposable anotherDisposableResource) : base(disposableResource) { _anotherDisposableResource = anotherDisposableResource; } // NOTE: only implement a finalizer if you MUST (and only if the base class does not) //~CanonicalDisposableDerivedClass() //{ // Dispose(false); //} // public methods /// /// Another method. /// public void AnotherMethod() { ThrowIfDisposed(); // ... } // protected methods /// /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { // this method can be called multiple times // make sure your implementation of this method does not throw any exceptions if (!Disposed) { if (disposing) { // dispose of any managed disposable resources you own here if (_anotherDisposableResource != null) { _anotherDisposableResource.Dispose(); _anotherDisposableResource = null; // not strictly necessary but a good idea } } // dispose of any unmanaged resources here } base.Dispose(disposing); // call base Dispose last } } }