2
0

ThrowHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #nullable enable
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.CompilerServices;
  5. namespace Renci.SshNet.Common
  6. {
  7. internal static class ThrowHelper
  8. {
  9. public static void ThrowObjectDisposedIf(bool condition, object instance)
  10. {
  11. #if NET
  12. ObjectDisposedException.ThrowIf(condition, instance);
  13. #else
  14. if (condition)
  15. {
  16. Throw(instance);
  17. static void Throw(object? instance)
  18. {
  19. throw new ObjectDisposedException(instance?.GetType().FullName);
  20. }
  21. }
  22. #endif
  23. }
  24. public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
  25. {
  26. #if NET
  27. ArgumentNullException.ThrowIfNull(argument, paramName);
  28. #else
  29. if (argument is null)
  30. {
  31. Throw(paramName);
  32. [DoesNotReturn]
  33. static void Throw(string? paramName)
  34. {
  35. throw new ArgumentNullException(paramName);
  36. }
  37. }
  38. #endif
  39. }
  40. public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
  41. {
  42. #if NET
  43. ArgumentException.ThrowIfNullOrWhiteSpace(argument, paramName);
  44. #else
  45. if (string.IsNullOrWhiteSpace(argument))
  46. {
  47. Throw(argument, paramName);
  48. [DoesNotReturn]
  49. static void Throw(string? argument, string? paramName)
  50. {
  51. ThrowIfNull(argument, paramName);
  52. throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
  53. }
  54. }
  55. #endif
  56. }
  57. public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
  58. {
  59. #if NET
  60. ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
  61. #else
  62. if (string.IsNullOrEmpty(argument))
  63. {
  64. Throw(argument, paramName);
  65. [DoesNotReturn]
  66. static void Throw(string? argument, string? paramName)
  67. {
  68. ThrowIfNull(argument, paramName);
  69. throw new ArgumentException("The value cannot be an empty string.", paramName);
  70. }
  71. }
  72. #endif
  73. }
  74. #if !NET
  75. // A rough copy of
  76. // https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L960C13-L974C10
  77. // for lower targets.
  78. public static void ValidateBufferArguments(byte[] buffer, int offset, int count)
  79. {
  80. ThrowIfNull(buffer);
  81. ThrowIfNegative(offset);
  82. if ((uint)count > buffer.Length - offset)
  83. {
  84. Throw();
  85. [DoesNotReturn]
  86. static void Throw()
  87. {
  88. throw new ArgumentOutOfRangeException(
  89. nameof(count),
  90. "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
  91. }
  92. }
  93. }
  94. #endif
  95. public static void ThrowIfNegative(long value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
  96. {
  97. #if NET
  98. ArgumentOutOfRangeException.ThrowIfNegative(value, paramName);
  99. #else
  100. if (value < 0)
  101. {
  102. Throw(value, paramName);
  103. [DoesNotReturn]
  104. static void Throw(long value, string? paramName)
  105. {
  106. throw new ArgumentOutOfRangeException(paramName, value, "Value must be non-negative.");
  107. }
  108. }
  109. #endif
  110. }
  111. }
  112. }