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 all 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
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 @@ -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 @@ -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 @@ -54,10 +54,7 @@ public ReferralChasingOptions ReferralChasing

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

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);
}
if (_connection._disposed) throw new ObjectDisposedException(GetType().Name);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,7 @@ private void Restart()
private void StartRaisingEventsIfNotDisposed()
{
//Cannot allocate the directoryHandle and the readBuffer if the object has been disposed; finalization has been suppressed.
if (_disposed)
throw new ObjectDisposedException(GetType().Name);
ObjectDisposedException.ThrowIf(_disposed, this);
StartRaisingEvents();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,7 @@ public void Close()

internal void CheckDisposed()
{
if (_state == State.Closed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_state == State.Closed, this);
}

private enum State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ public void SetCookie(Cookie cookie)

private void CheckDisposed()
{
if (Disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(Disposed, this);
}

private void CheckSentHeaders()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,7 @@ private Uri RequestUri

internal void CheckDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
ObjectDisposedException.ThrowIf(_isDisposed, this);
}

private bool SupportsWebSockets => WebSocketProtocolComponent.IsSupported;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,7 @@ private void ThrowIfPendingException()

private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_isDisposed, this);
}

private void UpdateReceiveState(int newReceiveState, int expectedReceiveState)
Expand Down Expand Up @@ -2194,10 +2191,7 @@ public void Dispose()

private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_isDisposed, this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,7 @@ internal void StartOperationCommon(WebSocketHttpListenerDuplexStream currentStre
if (Interlocked.CompareExchange(ref _operating, InProgress, Free) != Free)
{
// If it was already "in-use" check if Dispose was called.
if (_disposeCalled)
{
// Dispose was called - throw ObjectDisposed.
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposeCalled, this);

Debug.Fail("Only one outstanding async operation is allowed per HttpListenerAsyncEventArgs instance.");
// Only one at a time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public LinkedResourceCollection LinkedResources
{
get
{
if (disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(disposed, this);

return _linkedResources ??= new LinkedResourceCollection();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,22 @@ public void Dispose()

protected override void RemoveItem(int index)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.RemoveItem(index);
}

protected override void ClearItems()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.ClearItems();
}


protected override void SetItem(int index, AlternateView item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand All @@ -65,10 +56,7 @@ protected override void SetItem(int index, AlternateView item)

protected override void InsertItem(int index, AlternateView item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ public Stream ContentStream
{
get
{
if (disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(disposed, this);

return _part.Stream!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,21 @@ public void Dispose()

protected override void RemoveItem(int index)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.RemoveItem(index);
}

protected override void ClearItems()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.ClearItems();
}

protected override void SetItem(int index, Attachment item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand All @@ -65,10 +56,7 @@ protected override void SetItem(int index, Attachment item)

protected override void InsertItem(int index, Attachment item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,21 @@ public void Dispose()

protected override void RemoveItem(int index)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.RemoveItem(index);
}

protected override void ClearItems()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

base.ClearItems();
}

protected override void SetItem(int index, LinkedResource item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand All @@ -64,10 +55,7 @@ protected override void SetItem(int index, LinkedResource item)

protected override void InsertItem(int index, LinkedResource item)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

if (item == null)
{
Expand Down
10 changes: 2 additions & 8 deletions src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,7 @@ public AttachmentCollection Attachments
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

return _attachments ??= new AttachmentCollection();
}
Expand All @@ -300,10 +297,7 @@ public AlternateViewCollection AlternateViews
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
ObjectDisposedException.ThrowIf(_disposed, this);

return _views ??= new AlternateViewCollection();
}
Expand Down
Loading