|
3 | 3 |
|
4 | 4 | using System.Diagnostics; |
5 | 5 | using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Globalization; |
| 7 | +using System.Resources; |
6 | 8 | using System.Runtime.CompilerServices; |
7 | 9 | using System.Runtime.InteropServices; |
8 | 10 |
|
@@ -73,6 +75,70 @@ public static void Operation([DoesNotReturnIf(false)] bool condition, [Interpola |
73 | 75 | } |
74 | 76 | } |
75 | 77 |
|
| 78 | + /// <inheritdoc cref="Operation(bool, ResourceManager, string, object?)"/> |
| 79 | + /// <param name="condition"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='condition']"/></param> |
| 80 | + /// <param name="resourceManager"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='resourceManager']"/></param> |
| 81 | + /// <param name="resourceName"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='unformattedMessageResourceName']"/></param> |
| 82 | + [DebuggerStepThrough] |
| 83 | + public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string resourceName) |
| 84 | + { |
| 85 | + Requires.NotNull(resourceManager, nameof(resourceManager)); |
| 86 | + if (!condition) |
| 87 | + { |
| 88 | + throw new InvalidOperationException(resourceManager.GetString(resourceName, CultureInfo.CurrentCulture)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <inheritdoc cref="Operation(bool, ResourceManager, string, object?, object?)"/> |
| 93 | + [DebuggerStepThrough] |
| 94 | + public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, object? arg1) |
| 95 | + { |
| 96 | + Requires.NotNull(resourceManager, nameof(resourceManager)); |
| 97 | + if (!condition) |
| 98 | + { |
| 99 | + throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, arg1)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <inheritdoc cref="Operation(bool, ResourceManager, string, object?[])"/> |
| 104 | + /// <param name="condition"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='condition']"/></param> |
| 105 | + /// <param name="resourceManager"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='resourceManager']"/></param> |
| 106 | + /// <param name="unformattedMessageResourceName"><inheritdoc cref="Operation(bool, ResourceManager, string, object?[])" path="/param[@name='unformattedMessageResourceName']"/></param> |
| 107 | + /// <param name="arg1">The first formatting argument.</param> |
| 108 | + /// <param name="arg2">The second formatting argument.</param> |
| 109 | + [DebuggerStepThrough] |
| 110 | + public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, object? arg1, object? arg2) |
| 111 | + { |
| 112 | + Requires.NotNull(resourceManager, nameof(resourceManager)); |
| 113 | + if (!condition) |
| 114 | + { |
| 115 | + throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, arg1, arg2)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Throws an <see cref="InvalidOperationException"/> if a condition is false. |
| 121 | + /// </summary> |
| 122 | + /// <param name="condition">The condition to check.</param> |
| 123 | + /// <param name="resourceManager">The resource manager from which to retrieve the exception message. For example: <c>Strings.ResourceManager</c>.</param> |
| 124 | + /// <param name="unformattedMessageResourceName">The name of the string resource to obtain for the exception message. For example: <c>nameof(Strings.SomeError)</c>.</param> |
| 125 | + /// <param name="args">The formatting arguments.</param> |
| 126 | + /// <remarks> |
| 127 | + /// This overload allows only loading a localized string in the error condition as an optimization in perf critical sections over the simpler |
| 128 | + /// to use <see cref="Operation(bool, string?)"/> overload. |
| 129 | + /// </remarks> |
| 130 | + /// <exception cref="ArgumentNullException">Thrown if <paramref name="resourceManager"/> is <see langword="null"/>.</exception> |
| 131 | + /// <exception cref="InvalidOperationException">Thrown if <paramref name="condition"/> is <see langword="false"/>.</exception> |
| 132 | + [DebuggerStepThrough] |
| 133 | + public static void Operation([DoesNotReturnIf(false)] bool condition, ResourceManager resourceManager, string unformattedMessageResourceName, params object?[] args) |
| 134 | + { |
| 135 | + Requires.NotNull(resourceManager, nameof(resourceManager)); |
| 136 | + if (!condition) |
| 137 | + { |
| 138 | + throw new InvalidOperationException(PrivateErrorHelpers.Format(resourceManager.GetString(unformattedMessageResourceName, CultureInfo.CurrentCulture)!, args)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
76 | 142 | /// <summary> |
77 | 143 | /// Throws an <see cref="InvalidOperationException"/> if a condition is false. |
78 | 144 | /// </summary> |
|
0 commit comments