浏览代码

Using nameof operator

+ One correction of an argument reference in an argument exception

Source commit: f76f49ae129bd3a64fb71c3bbbfadd44dcbcbbb1
Martin Prikryl 6 年之前
父节点
当前提交
79678dd885
共有 4 个文件被更改,包括 34 次插入34 次删除
  1. 12 12
      dotnet/RemotePath.cs
  2. 12 12
      dotnet/Session.cs
  3. 9 9
      dotnet/SessionOptions.cs
  4. 1 1
      dotnet/TransferResumeSupport.cs

+ 12 - 12
dotnet/RemotePath.cs

@@ -14,7 +14,7 @@ namespace WinSCP
         {
             if (fileMask == null)
             {
-                throw new ArgumentNullException("fileMask");
+                throw new ArgumentNullException(nameof(fileMask));
             }
             int lastSlash = fileMask.LastIndexOf('/');
             string path = lastSlash > 0 ? fileMask.Substring(0, lastSlash + 1) : string.Empty;
@@ -34,12 +34,12 @@ namespace WinSCP
         {
             if (path1 == null)
             {
-                throw new ArgumentNullException("path1");
+                throw new ArgumentNullException(nameof(path1));
             }
 
             if (path2 == null)
             {
-                throw new ArgumentNullException("path2");
+                throw new ArgumentNullException(nameof(path2));
             }
 
             string result;
@@ -62,17 +62,17 @@ namespace WinSCP
         {
             if (remotePath == null)
             {
-                throw new ArgumentNullException("remotePath");
+                throw new ArgumentNullException(nameof(remotePath));
             }
 
             if (remoteRoot == null)
             {
-                throw new ArgumentNullException("remoteRoot");
+                throw new ArgumentNullException(nameof(remoteRoot));
             }
 
             if (localRoot == null)
             {
-                throw new ArgumentNullException("localRoot");
+                throw new ArgumentNullException(nameof(localRoot));
             }
 
             if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
@@ -115,17 +115,17 @@ namespace WinSCP
         {
             if (localPath == null)
             {
-                throw new ArgumentNullException("localPath");
+                throw new ArgumentNullException(nameof(localPath));
             }
 
             if (localRoot == null)
             {
-                throw new ArgumentNullException("localRoot");
+                throw new ArgumentNullException(nameof(localRoot));
             }
 
             if (remoteRoot == null)
             {
-                throw new ArgumentNullException("remoteRoot");
+                throw new ArgumentNullException(nameof(remoteRoot));
             }
 
             if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
@@ -173,7 +173,7 @@ namespace WinSCP
             }
             else if (path.Length == 0)
             {
-                throw new ArgumentException("Path cannot be empty", "path");
+                throw new ArgumentException("Path cannot be empty", nameof(path));
             }
             else
             {
@@ -205,7 +205,7 @@ namespace WinSCP
         {
             if (string.IsNullOrEmpty(path))
             {
-                throw new ArgumentException("Path cannot be empty", "path");
+                throw new ArgumentException("Path cannot be empty", nameof(path));
             }
 
             if (!path.EndsWith("/", StringComparison.Ordinal))
@@ -292,7 +292,7 @@ namespace WinSCP
         {
             if (target == null)
             {
-                throw new ArgumentNullException("target");
+                throw new ArgumentNullException(nameof(target));
             }
 
             Type type = target.GetType();

+ 12 - 12
dotnet/Session.cs

@@ -231,7 +231,7 @@ namespace WinSCP
 
                 if (sessionOptions == null)
                 {
-                    throw Logger.WriteException(new ArgumentNullException("sessionOptions"));
+                    throw Logger.WriteException(new ArgumentNullException(nameof(sessionOptions)));
                 }
 
                 try
@@ -402,7 +402,7 @@ namespace WinSCP
                 string normalizeAlgorithm = NormalizeIdent(algorithm);
                 if (string.IsNullOrEmpty(normalizeAlgorithm))
                 {
-                    throw Logger.WriteException(new ArgumentException("Algorithm cannot be empty", "algorithm"));
+                    throw Logger.WriteException(new ArgumentException("Algorithm cannot be empty", nameof(algorithm)));
                 }
 
                 string result;
@@ -765,11 +765,11 @@ namespace WinSCP
             {
                 if (localDirectory == null)
                 {
-                    throw Logger.WriteException(new ArgumentNullException("localDirectory"));
+                    throw Logger.WriteException(new ArgumentNullException(nameof(localDirectory)));
                 }
                 if (remoteDirectory == null)
                 {
-                    throw Logger.WriteException(new ArgumentNullException("remoteDirectory"));
+                    throw Logger.WriteException(new ArgumentNullException(nameof(remoteDirectory)));
                 }
                 if (string.IsNullOrEmpty(filemask))
                 {
@@ -791,7 +791,7 @@ namespace WinSCP
             {
                 if (string.IsNullOrEmpty(localFilePath))
                 {
-                    throw Logger.WriteException(new ArgumentException("File to path cannot be empty", "localFilePath"));
+                    throw Logger.WriteException(new ArgumentException("File to path cannot be empty", nameof(localFilePath)));
                 }
 
                 if (!File.Exists(localFilePath))
@@ -902,11 +902,11 @@ namespace WinSCP
             {
                 if (remoteDirectory == null)
                 {
-                    throw Logger.WriteException(new ArgumentNullException("remoteDirectory"));
+                    throw Logger.WriteException(new ArgumentNullException(nameof(remoteDirectory)));
                 }
                 if (localDirectory == null)
                 {
-                    throw Logger.WriteException(new ArgumentNullException("localDirectory"));
+                    throw Logger.WriteException(new ArgumentNullException(nameof(localDirectory)));
                 }
                 if (string.IsNullOrEmpty(filemask))
                 {
@@ -931,7 +931,7 @@ namespace WinSCP
             {
                 if (string.IsNullOrEmpty(remoteFilePath))
                 {
-                    throw Logger.WriteException(new ArgumentException("File to path cannot be empty", "remoteDirectory"));
+                    throw Logger.WriteException(new ArgumentException("File to path cannot be empty", nameof(remoteFilePath)));
                 }
 
                 string remoteDirectory = RemotePath.GetDirectoryName(remoteFilePath);
@@ -1031,7 +1031,7 @@ namespace WinSCP
                     modeName = "both";
                     break;
                 default:
-                    throw Logger.WriteException(new ArgumentOutOfRangeException("mode"));
+                    throw Logger.WriteException(new ArgumentOutOfRangeException(nameof(mode)));
             }
 
             string criteriaName;
@@ -1050,7 +1050,7 @@ namespace WinSCP
                     criteriaName = "either";
                     break;
                 default:
-                    throw Logger.WriteException(new ArgumentOutOfRangeException("criteria"));
+                    throw Logger.WriteException(new ArgumentOutOfRangeException(nameof(criteria)));
             }
 
             WriteCommand(
@@ -2396,7 +2396,7 @@ namespace WinSCP
 
                     if (target == null)
                     {
-                        throw Logger.WriteException(new ArgumentNullException("target"));
+                        throw Logger.WriteException(new ArgumentNullException(nameof(target)));
                     }
 
                     Type type = target.GetType();
@@ -2424,7 +2424,7 @@ namespace WinSCP
 
                             if (args == null)
                             {
-                                throw Logger.WriteException(new ArgumentNullException("args"));
+                                throw Logger.WriteException(new ArgumentNullException(nameof(args)));
                             }
 
                             if (method != null)

+ 9 - 9
dotnet/SessionOptions.cs

@@ -91,7 +91,7 @@ namespace WinSCP
         {
             if (url == null)
             {
-                throw new ArgumentNullException("url");
+                throw new ArgumentNullException(nameof(url));
             }
 
             url = url.Trim();
@@ -99,13 +99,13 @@ namespace WinSCP
             int index = url.IndexOf(protocolSeparator, StringComparison.OrdinalIgnoreCase);
             if (index < 0)
             {
-                throw new ArgumentException("Protocol not specified", "url");
+                throw new ArgumentException("Protocol not specified", nameof(url));
             }
 
             string protocol = url.Substring(0, index).Trim();
             if (!ParseProtocol(protocol))
             {
-                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unknown protocol {0}", protocol), "url");
+                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unknown protocol {0}", protocol), nameof(url));
             }
 
             url = url.Substring(index + protocolSeparator.Length).Trim();
@@ -121,7 +121,7 @@ namespace WinSCP
                 {
                     if ((Protocol != Protocol.Webdav) && (Protocol != Protocol.S3))
                     {
-                        throw new ArgumentException("Root path can be specified for WebDAV and S3 protocols only", "url");
+                        throw new ArgumentException("Root path can be specified for WebDAV and S3 protocols only", nameof(url));
                     }
                     RootPath = path;
                 }
@@ -129,7 +129,7 @@ namespace WinSCP
                 // forward compatibility
                 if (!string.IsNullOrEmpty(parameters))
                 {
-                    throw new ArgumentException("No session parameters are supported", "url");
+                    throw new ArgumentException("No session parameters are supported", nameof(url));
                 }
             }
 
@@ -157,7 +157,7 @@ namespace WinSCP
                 {
                     if (hostInfo[0] != ':')
                     {
-                        throw new ArgumentException("Unexpected syntax after ]", "url");
+                        throw new ArgumentException("Unexpected syntax after ]", nameof(url));
                     }
                     else
                     {
@@ -173,7 +173,7 @@ namespace WinSCP
 
             if (string.IsNullOrEmpty(HostName))
             {
-                throw new ArgumentException("No host name", "url");
+                throw new ArgumentException("No host name", nameof(url));
             }
 
             if (string.IsNullOrEmpty(portNumber))
@@ -185,7 +185,7 @@ namespace WinSCP
                 portNumber = UriUnescape(portNumber);
                 if (!int.TryParse(portNumber, 0, CultureInfo.InvariantCulture, out int number))
                 {
-                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0} is not a valid port number", portNumber), "url");
+                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "{0} is not a valid port number", portNumber), nameof(url));
                 }
                 else
                 {
@@ -232,7 +232,7 @@ namespace WinSCP
                     }
                     else
                     {
-                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unsupported connection parameter {0}", parameterName), "url");
+                        throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unsupported connection parameter {0}", parameterName), nameof(url));
                     }
                 }
             }

+ 1 - 1
dotnet/TransferResumeSupport.cs

@@ -67,7 +67,7 @@ namespace WinSCP
             {
                 if (threshold <= 0)
                 {
-                    throw new ArgumentOutOfRangeException("threshold", "Threshold must be positive");
+                    throw new ArgumentOutOfRangeException(nameof(threshold), "Threshold must be positive");
                 }
                 State = TransferResumeSupportState.Smart;
                 _threshold = threshold;