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); } } }
public void BitwiseClearTest() { { int a = 0; Assert.False(InterlockedUtilty.BitwiseClear(ref a, 1)); Assert.Equal(0, a); } { int a = 0; Assert.False(InterlockedUtilty.BitwiseClear(ref a, 2)); Assert.Equal(0, a); } { int b = 1; Assert.True(InterlockedUtilty.BitwiseClear(ref b, 1)); Assert.Equal(0, b); } { int b = 1; Assert.False(InterlockedUtilty.BitwiseClear(ref b, 2)); Assert.Equal(1, b); } }