| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | |
|
| | 6 | | namespace mkmrk.Channels; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Utility class for throwing exceptions in an efficient way. |
| | 10 | | /// <para /> |
| | 11 | | /// While there are <a href="https://stackoverflow.com/questions/1980044/when-should-i-use-a-throwhelper-method-instead- |
| | 12 | | /// the consensus amongst .NET runtime developers is that it is the correct way to go. |
| | 13 | | /// The runtime docs say <a href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/S |
| | 14 | | /// <i>It is very important we do this for generic classes because we can easily generate the same code multiple times f |
| | 15 | | /// <br/> |
| | 16 | | /// To facilitate use in methods or locations that must return a value, use one of the Generic helper methods, eg <c>pub |
| | 17 | | /// See <a href="https://learn.microsoft.com/en-us/dotnet/communitytoolkit/diagnostics/throwhelper#:~:text=within%20expr |
| | 18 | | /// |
| | 19 | | /// <para/>There is one large downside, <b>the stack trace will start at the ThrowHelper method, not the method that cal |
| | 20 | | /// <para /> |
| | 21 | | /// For background see: |
| | 22 | | /// <list type="bullet"> |
| | 23 | | /// <item> |
| | 24 | | /// <description>https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Thr |
| | 25 | | /// </item> |
| | 26 | | /// <item> |
| | 27 | | /// <description>https://learn.microsoft.com/en-us/dotnet/communitytoolkit/diagnostics/throwhelper</description> |
| | 28 | | /// </item> |
| | 29 | | /// <item> |
| | 30 | | /// <description>https://dunnhq.com/posts/2022/throw-helper/</description> |
| | 31 | | /// </item> |
| | 32 | | /// </list> |
| | 33 | | /// </summary> |
| | 34 | | [ StackTraceHidden ] |
| | 35 | | internal static class ThrowHelper { |
| | 36 | | /// <inheritdoc cref="ObjectDisposedException" /> |
| | 37 | | [ DoesNotReturn ] |
| | 38 | | internal static TReturn ThrowObjectDisposedException<TReturn>( string typeNameDisposed ) => |
| 0 | 39 | | throw new ObjectDisposedException( typeNameDisposed ); |
| | 40 | |
|
| | 41 | | /// <inheritdoc cref="ObjectDisposedException" /> |
| | 42 | | [ DoesNotReturn ] |
| | 43 | | internal static bool ThrowObjectDisposedException<TOut>( string typeNameDisposed, out TOut data ) => |
| 0 | 44 | | throw new ObjectDisposedException( typeNameDisposed ); |
| | 45 | |
|
| | 46 | | /// <inheritdoc cref="KeyNotFoundException" /> |
| | 47 | | [ DoesNotReturn ] |
| | 48 | | internal static void ThrowKeyNotFoundException( string message ) => |
| 0 | 49 | | throw new System.Collections.Generic.KeyNotFoundException( message ); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Throw <see cref="InvalidCastException"/>, supplying the destination type for the message |
| | 53 | | /// </summary> |
| | 54 | | [ DoesNotReturn ] |
| | 55 | | internal static TCast ThrowInvalidCastException<TInput, TCast>( TInput? variable ) |
| 0 | 56 | | => throw new InvalidCastException( $"Unable to cast type {typeof(TInput).Name} with value {variable} to type {ty |
| | 57 | | } |