Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectDisposedException.Throw for object instance and type #58684

Merged
merged 21 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/libraries/Common/tests/System/IO/PositionValueStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public PositionValueStream(int totalCount)

public override int Read(byte[] buffer, int offset, int count)
{
if (_remaining < 0)
{
throw new ObjectDisposedException(typeof(PositionValueStream).Name);
}
ObjectDisposedException.ThrowIf(_remaining < 0, this);

if (_remaining == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,7 @@ public override void SetLength(long value)

private void ThrowIfDisposed()
{
if (_disposed != 0)
{
ThrowObjectDisposedException();
}

void ThrowObjectDisposedException() => throw new ObjectDisposedException(GetType().FullName);
ObjectDisposedException.ThrowIf(_disposed != 0, this);
}

private static IOException WrapException(string resourceFormatString, Exception innerException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,7 @@ private Export CreateExport(ComposablePart part, ExportDefinition export)
[DebuggerStepThrough]
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_isDisposed, this);
}

[DebuggerStepThrough]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,12 @@ public int Value
{
get
{
if (this.IsDisposed)
throw new ObjectDisposedException(this.GetType().Name);
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
return this._value;
}
set
{
if (this.IsDisposed)
throw new ObjectDisposedException(this.GetType().Name);
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
this._value = value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ public bool Enabled
_enabled = value;
if (_timer == null)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

int i = (int)Math.Ceiling(_interval);
_cookie = new object();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,8 +1046,7 @@ public void ModifyOverflowPolicy(OverflowAction action, int retentionDays)

private void OpenForRead(string currentMachineName)
{
if (this.boolFlags[Flag_disposed])
throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(this.boolFlags[Flag_disposed], this);
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

string logname = GetLogName(currentMachineName);

Expand Down Expand Up @@ -1083,7 +1082,7 @@ private void OpenForWrite(string currentMachineName)
{
//Cannot allocate the writeHandle if the object has been disposed, since finalization has been suppressed.
if (this.boolFlags[Flag_disposed])
throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(this.boolFlags[Flag_disposed], this);

if (sourceName == null || sourceName.Length == 0)
throw new ArgumentException(SR.NeedSourceToOpen);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1691,10 +1691,7 @@ internal void ErrorReadNotifyUser(string? data)
/// <exception cref="System.ObjectDisposedException">If the Proces has been disposed.</exception>
private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);
}

private static Win32Exception CreateExceptionForErrorStartingProcess(string errorMessage, int errorCode, string fileName, string? workingDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ public override DirectoryResponse SendRequest(DirectoryRequest request)

public DirectoryResponse SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (request == null)
{
Expand Down Expand Up @@ -291,10 +288,7 @@ public IAsyncResult BeginSendRequest(DirectoryRequest request, PartialResultProc

public IAsyncResult BeginSendRequest(DirectoryRequest request, TimeSpan requestTimeout, PartialResultProcessing partialMode, AsyncCallback callback, object state)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (request == null)
{
Expand Down Expand Up @@ -411,10 +405,7 @@ static async Task ResponseCallback(ValueTask<DirectoryResponse> vt, LdapRequestS

public void Abort(IAsyncResult asyncResult)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (asyncResult == null)
{
Expand Down Expand Up @@ -459,10 +450,7 @@ public void Abort(IAsyncResult asyncResult)

public PartialResultsCollection GetPartialResults(IAsyncResult asyncResult)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (asyncResult == null)
{
Expand All @@ -484,10 +472,7 @@ public PartialResultsCollection GetPartialResults(IAsyncResult asyncResult)

public DirectoryResponse EndSendRequest(IAsyncResult asyncResult)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (asyncResult == null)
{
Expand Down Expand Up @@ -1028,10 +1013,7 @@ private void Connect()

private void BindHelper(NetworkCredential newCredential, bool needSetCredential)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_disposed, this);

// Throw if user wants to do anonymous bind but specifies credentials.
if (AuthType == AuthType.Anonymous && (newCredential != null && (!string.IsNullOrEmpty(newCredential.Password) || string.IsNullOrEmpty(newCredential.UserName))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public bool SecureSocketLayer
{
get
{
if (_connection._disposed) throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(_connection._disposed, this);
return _secureSocketLayer;
}
set
{
if (_connection._disposed) throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(_connection._disposed, this);
_secureSocketLayer = value;
}
}
Expand Down Expand Up @@ -54,10 +54,7 @@ public ReferralChasingOptions ReferralChasing

private bool GetBoolValueHelper(LdapOption option)
{
if (_connection._disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_connection._disposed, this);

bool outValue = false;
int error = LdapPal.GetBoolOption(_connection._ldapHandle, option, ref outValue);
Expand All @@ -68,10 +65,7 @@ private bool GetBoolValueHelper(LdapOption option)

private void SetBoolValueHelper(LdapOption option, bool value)
{
if (_connection._disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
ObjectDisposedException.ThrowIf(_connection._disposed, this);

int error = LdapPal.SetBoolOption(_connection._ldapHandle, option, value);

Expand Down
Loading