// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Runtime.Serialization;
namespace CefSharp
{
/// Based on: http://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx
///
/// Provides base class functionality for implementing the Dispose Pattern.
///
[DataContract]
public abstract class DisposableResource : IDisposable
{
~DisposableResource()
{
DoDispose(false);
IsDisposed = true;
}
public void Dispose()
{
DoDispose(true);
IsDisposed = true;
GC.SuppressFinalize(this);
}
protected virtual void DisposeMember(ref T field)
where T : class, IDisposable
{
var oldvalue = field;
field = null;
if (oldvalue != null)
{
oldvalue.Dispose();
}
}
protected virtual void DoDispose(bool isDisposing)
{
}
///
/// If this Object is disposed the Method will throw a .
/// Use this method to guard methods that are not allowed to be called after dispose was called
///
/// Thrown when this resource is disposed.
protected virtual void DisposeGuard()
{
if (IsDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
}
///
/// Gets a value indicating whether this instance is disposed.
/// This Value Should be used for lifetime management.
/// i.e. EventHandlers may not be registered if this object is already disposed
///
///
/// true if this instance is disposed; otherwise, false.
///
public bool IsDisposed { get; private set; }
}
}