From ca21c617c7ba72d2e7674188e7a6b81970103601 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 16 Jul 2021 15:07:08 +0100 Subject: [PATCH 1/4] Implement `IDisposable` --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 300888009bd..3816514ec24 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -16,7 +16,7 @@ namespace Microsoft.PowerShell.Commands /// This command converts an object to a Json string representation. /// [Cmdlet(VerbsData.ConvertTo, "Json", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096925", RemotingCapability = RemotingCapability.None)] - public class ConvertToJsonCommand : PSCmdlet + public class ConvertToJsonCommand : PSCmdlet, IDisposable { /// /// Gets or sets the InputObject property. @@ -31,6 +31,8 @@ public class ConvertToJsonCommand : PSCmdlet private readonly CancellationTokenSource _cancellationSource = new(); + private bool _disposed; + /// /// Gets or sets the Depth property. /// @@ -77,6 +79,34 @@ public int Depth [Parameter] public StringEscapeHandling EscapeHandling { get; set; } = StringEscapeHandling.Default; + /// + /// IDisposable implementation, dispose of any disposable resources created by the cmdlet. + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + /// + /// Implementation of IDisposable for both manual Dispose() and finalizer-called disposal of resources. + /// + /// + /// Specified as true when Dispose() was called, false if this is called from the finalizer. + /// + protected virtual void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing) + { + _cancellationSource.Dispose(); + } + + _disposed = true; + } + } + /// /// Prerequisite checks. /// From f3fad433fa037175a02fde1a1e31d80be516ae7e Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 16 Jul 2021 16:14:21 -0700 Subject: [PATCH 2/4] Minor update --- .../utility/WebCmdlet/ConvertToJsonCommand.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index 3816514ec24..caf478bf24a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -96,13 +96,14 @@ public void Dispose() /// protected virtual void Dispose(bool disposing) { - if (!_disposed) + if (_disposed) { - if (disposing) - { - _cancellationSource.Dispose(); - } - + return; + } + + if (disposing) + { + _cancellationSource.Dispose(); _disposed = true; } } From 29cdfb47808fbdc28c8be90e46684303edeed6ab Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 16 Jul 2021 16:21:02 -0700 Subject: [PATCH 3/4] Minor update - 2 --- .../commands/utility/WebCmdlet/ConvertToJsonCommand.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index caf478bf24a..f5d75124522 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -104,8 +104,9 @@ protected virtual void Dispose(bool disposing) if (disposing) { _cancellationSource.Dispose(); - _disposed = true; } + + _disposed = true; } /// From 83a452fec943fdf83011c1bcbe7e1134adfb9ab6 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 18 Jul 2021 10:39:08 +0100 Subject: [PATCH 4/4] Remove `_disposed` bool --- .../commands/utility/WebCmdlet/ConvertToJsonCommand.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs index f5d75124522..36e8acdfd6e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/ConvertToJsonCommand.cs @@ -31,8 +31,6 @@ public class ConvertToJsonCommand : PSCmdlet, IDisposable private readonly CancellationTokenSource _cancellationSource = new(); - private bool _disposed; - /// /// Gets or sets the Depth property. /// @@ -96,17 +94,10 @@ public void Dispose() /// protected virtual void Dispose(bool disposing) { - if (_disposed) - { - return; - } - if (disposing) { _cancellationSource.Dispose(); } - - _disposed = true; } ///