diff --git a/src/libraries/Common/tests/System/Net/WebSockets/WebSocketStream.cs b/src/libraries/Common/tests/System/Net/WebSockets/WebSocketStream.cs index 322f5d2a00cb0..f025a7977f28b 100644 --- a/src/libraries/Common/tests/System/Net/WebSockets/WebSocketStream.cs +++ b/src/libraries/Common/tests/System/Net/WebSockets/WebSocketStream.cs @@ -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) diff --git a/src/libraries/System.ComponentModel.Composition/tests/System/Integration/LifetimeTests.cs b/src/libraries/System.ComponentModel.Composition/tests/System/Integration/LifetimeTests.cs index c1a50da9ba1ed..941bb54d0e7db 100644 --- a/src/libraries/System.ComponentModel.Composition/tests/System/Integration/LifetimeTests.cs +++ b/src/libraries/System.ComponentModel.Composition/tests/System/Integration/LifetimeTests.cs @@ -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; } } diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/Timers/Timer.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/Timers/Timer.cs index b7e8fc9bf3195..0dc77a54ccda6 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/Timers/Timer.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/Timers/Timer.cs @@ -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(); diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs index 0c5bf3a96542a..43c7e289682f1 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs @@ -1691,10 +1691,7 @@ internal void ErrorReadNotifyUser(string? data) /// If the Proces has been disposed. 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) diff --git a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs index d2f3b72c1adf7..96661bd33a7b4 100644 --- a/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs +++ b/src/libraries/System.DirectoryServices.Protocols/src/System/DirectoryServices/Protocols/ldap/LdapSessionOptions.Linux.cs @@ -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); @@ -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); diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs index e3135500d8ce7..8ea51f94eaf6d 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs @@ -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(); } diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs index b391474791591..640b7f6bde309 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListener.cs @@ -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 diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs index 6b7ddc839b303..1568b66333746 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs @@ -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() diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs index c3b038a3f083f..175085f4140c4 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerRequest.Windows.cs @@ -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; diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs index 5161406961fe9..786eda54a9804 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketBase.cs @@ -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) @@ -2194,10 +2191,7 @@ public void Dispose() private void ThrowIfDisposed() { - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_isDisposed, this); } } diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketHttpListenerDuplexStream.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketHttpListenerDuplexStream.cs index 0e1845499519c..67437df45f46e 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketHttpListenerDuplexStream.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/WebSockets/WebSocketHttpListenerDuplexStream.cs @@ -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. diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateView.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateView.cs index fdad19f71afd5..029230c194049 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateView.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateView.cs @@ -42,10 +42,7 @@ public LinkedResourceCollection LinkedResources { get { - if (disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(disposed, this); return _linkedResources ??= new LinkedResourceCollection(); } diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateViewCollection.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateViewCollection.cs index bfe1649c9dddd..eae7b633d3969 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateViewCollection.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/AlternateViewCollection.cs @@ -29,20 +29,14 @@ 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(); } @@ -50,10 +44,7 @@ protected override void ClearItems() protected override void SetItem(int index, AlternateView item) { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); if (item == null) { @@ -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) { diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs index 5a275b5e002dd..0ea9aa12af67e 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/Attachment.cs @@ -215,10 +215,7 @@ public Stream ContentStream { get { - if (disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(disposed, this); return _part.Stream!; } diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/AttachmentCollection.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/AttachmentCollection.cs index 755d7b928d3c0..96be60c1fdcfb 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/AttachmentCollection.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/AttachmentCollection.cs @@ -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) { @@ -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) { diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/LinkedResourceCollection.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/LinkedResourceCollection.cs index 01e27c3daa6a7..c32b21197e91c 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/LinkedResourceCollection.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/LinkedResourceCollection.cs @@ -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) { @@ -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) { diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs index 7ec260a750a87..67db9ed4c3956 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/MailMessage.cs @@ -288,10 +288,7 @@ public AttachmentCollection Attachments { get { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); return _attachments ??= new AttachmentCollection(); } @@ -300,10 +297,7 @@ public AlternateViewCollection AlternateViews { get { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); return _views ??= new AlternateViewCollection(); } diff --git a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs index 415c1e6e972d9..1124aa6314d6d 100644 --- a/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs +++ b/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs @@ -415,10 +415,7 @@ private void SendCompletedWaitCallback(object? operationState) public void Send(string from, string recipients, string? subject, string? body) { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); //validation happends in MailMessage constructor MailMessage mailMessage = new MailMessage(from, recipients, subject, body); Send(mailMessage); @@ -426,10 +423,7 @@ public void Send(string from, string recipients, string? subject, string? body) public void Send(MailMessage message) { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); if (NetEventSource.Log.IsEnabled()) { @@ -569,19 +563,13 @@ e is AuthenticationException || public void SendAsync(string from, string recipients, string? subject, string? body, object? userToken) { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); SendAsync(new MailMessage(from, recipients, subject, body), userToken); } public void SendAsync(MailMessage message, object? userToken) { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); try @@ -722,10 +710,7 @@ private bool IsSystemNetworkCredentialInCache(CredentialCache cache) public void SendAsyncCancel() { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); if (!InCall || _cancelled) { diff --git a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs index fb16acbb3ec51..fcb658a462c2c 100644 --- a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs +++ b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.cs @@ -78,10 +78,7 @@ private void CheckArgs(IPAddress address, int timeout, byte[] buffer, PingOption private void CheckDisposed() { - if (_disposeRequested) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposeRequested, this); } private void CheckStart() diff --git a/src/libraries/System.Net.Requests/src/System/Net/FileWebResponse.cs b/src/libraries/System.Net.Requests/src/System/Net/FileWebResponse.cs index ab252369d3967..d02ef40fd17e6 100644 --- a/src/libraries/System.Net.Requests/src/System/Net/FileWebResponse.cs +++ b/src/libraries/System.Net.Requests/src/System/Net/FileWebResponse.cs @@ -101,10 +101,7 @@ public override Uri ResponseUri private void CheckDisposed() { - if (_closed) - { - throw new ObjectDisposedException(this.GetType().FullName); - } + ObjectDisposedException.ThrowIf(_closed, this); } public override void Close() diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs index e10f5b6b0fa26..e7b8b4e874530 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs @@ -669,12 +669,7 @@ internal void SetSocketTimeoutOption(SocketShutdown mode, int timeout, bool sile 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) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs index c857ab33a1eee..40b34ca83b2bc 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs @@ -3817,15 +3817,9 @@ private void UpdateAcceptSocketErrorForDisposed(ref SocketError socketError) private void ThrowIfDisposed() { - if (Disposed) - { - ThrowObjectDisposedException(); - } + ObjectDisposedException.ThrowIf(Disposed, this); } - [DoesNotReturn] - private void ThrowObjectDisposedException() => throw new ObjectDisposedException(GetType().FullName); - private bool IsConnectionOriented => _socketType == SocketType.Stream; internal static void SocketListDangerousReleaseRefs(IList? socketList, ref int refsAdded) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs index 06723ad87fe3a..51ffe1126953c 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPClient.cs @@ -453,12 +453,7 @@ private void InitializeClientSocket() private void ThrowIfDisposed() { - if (Disposed) - { - ThrowObjectDisposedException(); - } - - void ThrowObjectDisposedException() => throw new ObjectDisposedException(GetType().FullName); + ObjectDisposedException.ThrowIf(Disposed, this); } } } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs index 246483d0815fb..e662dcb648173 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/UDPClient.cs @@ -1090,12 +1090,7 @@ public int Send(ReadOnlySpan datagram) private void ThrowIfDisposed() { - if (_disposed) - { - ThrowObjectDisposedException(); - } - - void ThrowObjectDisposedException() => throw new ObjectDisposedException(GetType().FullName); + ObjectDisposedException.ThrowIf(_disposed, this); } } } diff --git a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs index b7b1b01f8b8d2..75935a7147103 100644 --- a/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs +++ b/src/libraries/System.Net.WebSockets.Client/src/System/Net/WebSockets/BrowserWebSockets/BrowserWebSocket.cs @@ -347,10 +347,7 @@ private async ValueTask CancelationHelper(Task jsTask, int promi private void ThrowIfDisposed() { - if (_disposed) - { - throw new ObjectDisposedException(GetType().FullName); - } + ObjectDisposedException.ThrowIf(_disposed, this); } private WebSocketState GetReadyState() @@ -368,4 +365,4 @@ private WebSocketState GetReadyState() }; } } -} +} \ No newline at end of file diff --git a/src/libraries/System.Private.CoreLib/src/System/ObjectDisposedException.cs b/src/libraries/System.Private.CoreLib/src/System/ObjectDisposedException.cs index 7a5a804faac47..da88f69339130 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ObjectDisposedException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ObjectDisposedException.cs @@ -3,6 +3,8 @@ using System.Runtime.CompilerServices; using System.Runtime.Serialization; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System { @@ -44,6 +46,24 @@ protected ObjectDisposedException(SerializationInfo info, StreamingContext conte _objectName = info.GetString("ObjectName"); } + [StackTraceHidden] + public static void ThrowIf([DoesNotReturnIf(true)] bool condition, object instance) + { + if (condition) + { + throw new ObjectDisposedException(instance?.GetType().FullName); + } + } + + [StackTraceHidden] + public static void ThrowIf([DoesNotReturnIf(true)] bool condition, Type type) + { + if (condition) + { + throw new ObjectDisposedException(type?.FullName); + } + } + public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); diff --git a/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.cs b/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.cs index 721c74149ca0a..21c6591dc711f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.cs @@ -308,10 +308,7 @@ private void EnsureNotReadOnly() private void EnsureNotDisposed() { - if (_buffer == null) - { - throw new ObjectDisposedException(GetType().Name); - } + ObjectDisposedException.ThrowIf(_buffer == null, this); } internal unsafe IntPtr MarshalToBSTR() diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 7393345702fe6..bd2e0d81869b2 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -4969,6 +4969,8 @@ public ObjectDisposedException(string? message, System.Exception? innerException public ObjectDisposedException(string? objectName, string? message) { } public override string Message { get { throw null; } } public string ObjectName { get { throw null; } } + public static void ThrowIf([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(true)] bool condition, object instance) => throw null; + public static void ThrowIf([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(true)] bool condition, Type type) => throw null; public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } } [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Constructor | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)] diff --git a/src/libraries/System.Runtime/tests/System/ObjectDisposedExceptionTests.cs b/src/libraries/System.Runtime/tests/System/ObjectDisposedExceptionTests.cs index d9168a213338d..82fb76a8050fc 100644 --- a/src/libraries/System.Runtime/tests/System/ObjectDisposedExceptionTests.cs +++ b/src/libraries/System.Runtime/tests/System/ObjectDisposedExceptionTests.cs @@ -42,5 +42,25 @@ public static void Ctor_String_String() Assert.Contains(message, exception.Message); Assert.Contains(objectName, exception.Message); } + + [Fact] + public static void Throw_Object() + { + var obj = new object(); + ObjectDisposedException ex = AssertExtensions.Throws( + () => ObjectDisposedException.ThrowIf(true, obj)); + + Assert.Equal("System.Object", ex.ObjectName); + } + + [Fact] + public static void Throw_Type() + { + Type type = new object().GetType(); + ObjectDisposedException ex = AssertExtensions.Throws( + () => ObjectDisposedException.ThrowIf(true, type)); + + Assert.Equal("System.Object", ex.ObjectName); + } } }