protected void SetResource(T resource)
        {
            if (ReferenceEquals(this._Resource, resource))
            {
                return;
            }
            bool oldResource = (this._Resource is object);
            bool newResource = (resource is object);

            using (var r = this._Resource) {
                this._Resource = resource;
            }
            if (oldResource && !newResource)
            {
                if (InterlockedUtilty.BitwiseSet(ref this._DisposeState, (int)(DisposeState.FinalizeSuppressed)))
                {
                    System.GC.SuppressFinalize(this);
                }
            }
            else if (!oldResource && newResource)
            {
                if (InterlockedUtilty.BitwiseClear(ref this._DisposeState, (int)(DisposeState.FinalizeSuppressed)))
                {
                    System.GC.ReRegisterForFinalize(this);
                }
            }
        }
        protected T ReadResourceAndForget()
        {
            var result = this._Resource;

            if (result is object)
            {
                this._Resource = null;
                if (InterlockedUtilty.BitwiseSet(ref this._DisposeState, (int)(DisposeState.FinalizeSuppressed)))
                {
                    System.GC.SuppressFinalize(this);
                }
            }
            return(result);
        }
 public void Dispose()
 {
     try {
         this.Dispose(disposing: true);
         InterlockedUtilty.BitwiseSet(ref this._DisposeState, (int)(DisposeState.Disposed));
     } catch {
         InterlockedUtilty.BitwiseSet(ref this._DisposeState, (int)(DisposeState.DisposedAndFaulted));
         throw;
     } finally {
         if (InterlockedUtilty.BitwiseSet(ref this._DisposeState, (int)(DisposeState.FinalizeSuppressed)))
         {
             System.GC.SuppressFinalize(this);
         }
     }
 }
 public void BitwiseSetTest()
 {
     {
         int a = 0;
         Assert.True(InterlockedUtilty.BitwiseSet(ref a, 1));
         Assert.Equal(1, a);
     }
     {
         int a = 2;
         Assert.True(InterlockedUtilty.BitwiseSet(ref a, 1));
         Assert.Equal(3, a);
     }
     {
         int b = 1;
         Assert.False(InterlockedUtilty.BitwiseSet(ref b, 1));
         Assert.Equal(1, b);
     }
     {
         int b = 3;
         Assert.False(InterlockedUtilty.BitwiseSet(ref b, 1));
         Assert.Equal(3, b);
     }
 }