Explorar o código

Move AuthenticationMethod IDisposable implementation declaration from inheritees to parent (#746)

* Move IDisposable implementation declaration from inheritees to parent AuthenticationMethod

* Move common Dispose code to AuthenticationMethod class

* Remove unnecessary finalizers

* just move the definition

---------

Co-authored-by: Michał Drzymała <michalxdrzymala@gmail.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
Michał Drzymała hai 9 meses
pai
achega
5c29394688

+ 19 - 1
src/Renci.SshNet/AuthenticationMethod.cs

@@ -7,7 +7,7 @@ namespace Renci.SshNet
     /// <summary>
     /// Base class for all supported authentication methods.
     /// </summary>
-    public abstract class AuthenticationMethod : IAuthenticationMethod
+    public abstract class AuthenticationMethod : IAuthenticationMethod, IDisposable
     {
         /// <summary>
         /// Gets the name of the authentication method.
@@ -61,5 +61,23 @@ namespace Renci.SshNet
         {
             return Authenticate((Session)session);
         }
+
+        /// <summary>
+        /// Releases unmanaged and - optionally - managed resources.
+        /// </summary>
+        /// <param name="disposing">
+        /// <see langword="true"/> to release both managed and unmanaged resources;
+        /// <see langword="false"/> to release only unmanaged resources.
+        /// </param>
+        protected virtual void Dispose(bool disposing)
+        {
+        }
+
+        /// <inheritdoc/>
+        public void Dispose()
+        {
+            Dispose(disposing: true);
+            GC.SuppressFinalize(this);
+        }
     }
 }

+ 5 - 15
src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

@@ -13,7 +13,7 @@ namespace Renci.SshNet
     /// <summary>
     /// Provides functionality to perform keyboard interactive authentication.
     /// </summary>
-    public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable
+    public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod
     {
         private readonly RequestMessageKeyboardInteractive _requestMessage;
         private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
@@ -151,20 +151,8 @@ namespace Renci.SshNet
                 });
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(disposing: true);
-            GC.SuppressFinalize(this);
-        }
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool disposing)
+        /// <inheritdoc/>
+        protected override void Dispose(bool disposing)
         {
             if (_isDisposed)
             {
@@ -182,6 +170,8 @@ namespace Renci.SshNet
 
                 _isDisposed = true;
             }
+
+            base.Dispose(disposing);
         }
     }
 }

+ 2 - 5
src/Renci.SshNet/KeyboardInteractiveConnectionInfo.cs

@@ -162,12 +162,9 @@ namespace Renci.SshNet
             {
                 if (AuthenticationMethods != null)
                 {
-                    foreach (var authenticationMethods in AuthenticationMethods)
+                    foreach (var authenticationMethod in AuthenticationMethods)
                     {
-                        if (authenticationMethods is IDisposable disposable)
-                        {
-                            disposable.Dispose();
-                        }
+                        authenticationMethod.Dispose();
                     }
                 }
 

+ 5 - 15
src/Renci.SshNet/NoneAuthenticationMethod.cs

@@ -10,7 +10,7 @@ namespace Renci.SshNet
     /// <summary>
     /// Provides functionality for "none" authentication method.
     /// </summary>
-    public class NoneAuthenticationMethod : AuthenticationMethod, IDisposable
+    public class NoneAuthenticationMethod : AuthenticationMethod
     {
         private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
         private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false);
@@ -87,20 +87,8 @@ namespace Renci.SshNet
             _ = _authenticationCompleted.Set();
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(disposing: true);
-            GC.SuppressFinalize(this);
-        }
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool disposing)
+        /// <inheritdoc/>
+        protected override void Dispose(bool disposing)
         {
             if (_isDisposed)
             {
@@ -118,6 +106,8 @@ namespace Renci.SshNet
 
                 _isDisposed = true;
             }
+
+            base.Dispose(disposing);
         }
     }
 }

+ 5 - 15
src/Renci.SshNet/PasswordAuthenticationMethod.cs

@@ -13,7 +13,7 @@ namespace Renci.SshNet
     /// <summary>
     /// Provides functionality to perform password authentication.
     /// </summary>
-    public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
+    public class PasswordAuthenticationMethod : AuthenticationMethod
     {
         private readonly RequestMessagePassword _requestMessage;
         private readonly byte[] _password;
@@ -163,20 +163,8 @@ namespace Renci.SshNet
             });
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(disposing: true);
-            GC.SuppressFinalize(this);
-        }
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool disposing)
+        /// <inheritdoc/>
+        protected override void Dispose(bool disposing)
         {
             if (_isDisposed)
             {
@@ -194,6 +182,8 @@ namespace Renci.SshNet
 
                 _isDisposed = true;
             }
+
+            base.Dispose(disposing);
         }
     }
 }

+ 1 - 4
src/Renci.SshNet/PasswordConnectionInfo.cs

@@ -281,10 +281,7 @@ namespace Renci.SshNet
                 {
                     foreach (var authenticationMethod in AuthenticationMethods)
                     {
-                        if (authenticationMethod is IDisposable disposable)
-                        {
-                            disposable.Dispose();
-                        }
+                        authenticationMethod.Dispose();
                     }
                 }
 

+ 5 - 15
src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs

@@ -13,7 +13,7 @@ namespace Renci.SshNet
     /// <summary>
     /// Provides functionality to perform private key authentication.
     /// </summary>
-    public class PrivateKeyAuthenticationMethod : AuthenticationMethod, IDisposable
+    public class PrivateKeyAuthenticationMethod : AuthenticationMethod
     {
         private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
         private EventWaitHandle _authenticationCompleted = new ManualResetEvent(initialState: false);
@@ -155,20 +155,8 @@ namespace Renci.SshNet
             _ = _authenticationCompleted.Set();
         }
 
-        /// <summary>
-        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
-        /// </summary>
-        public void Dispose()
-        {
-            Dispose(disposing: true);
-            GC.SuppressFinalize(this);
-        }
-
-        /// <summary>
-        /// Releases unmanaged and - optionally - managed resources.
-        /// </summary>
-        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
-        protected virtual void Dispose(bool disposing)
+        /// <inheritdoc/>
+        protected override void Dispose(bool disposing)
         {
             if (_isDisposed)
             {
@@ -186,6 +174,8 @@ namespace Renci.SshNet
 
                 _isDisposed = true;
             }
+
+            base.Dispose(disposing);
         }
 
         private sealed class SignatureData : SshData

+ 1 - 4
src/Renci.SshNet/PrivateKeyConnectionInfo.cs

@@ -160,10 +160,7 @@ namespace Renci.SshNet
                 {
                     foreach (var authenticationMethod in AuthenticationMethods)
                     {
-                        if (authenticationMethod is IDisposable disposable)
-                        {
-                            disposable.Dispose();
-                        }
+                        authenticationMethod.Dispose();
                     }
                 }