public void Set(IDisposable disposable)
{
if (disposable == null)
{
throw new ArgumentNullException("disposable");
}
object originalFieldValue = Interlocked.CompareExchange(ref _disposable, disposable, null);
if (originalFieldValue == null)
{
// this is the first call to Set() and Dispose() hasn't yet been called; do nothing
}
else if (originalFieldValue == _disposedSentinel)
{
// Dispose() has already been called, so we need to dispose of the object that was just added
disposable.Dispose();
}
else
{
#if !PORTABLE && !NETFX_CORE
// Set has been called multiple times, fail
Debug.Fail("Multiple calls to Disposer.Set(IDisposable) without calling Disposer.Dispose()");
#endif
}
}