Add and use Win32ExceptionExtensions throw helpers

このコミットが含まれているのは:
Sergio Pedri committed 2023-02-25 18:11:40 +01:00
1 親 fe41277fe7
コミット 72338416c2
5ファイル変更
+72 -25

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -18,7 +18,7 @@ internal static class ArgumentNullExceptionExtensions
[DoesNotReturn]
public static void Throw(this ArgumentNullException? _, string? parameterName)
{
Throw(parameterName);
throw new ArgumentNullException(parameterName);
}
/// <summary>
+61
ファイルの表示
@@ -0,0 +1,61 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.ComponentModel;
/// <summary>
/// Throw helper extensions for <see cref="Win32Exception"/>.
/// </summary>
internal static class Win32ExceptionExtensions
{
/// <summary>
/// Throws an <see cref="Win32Exception"/> for a given error code.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="hresult">The error code to report.</param>
[DoesNotReturn]
public static void Throw(this Win32Exception? _, int hresult)
{
throw new Win32Exception(hresult);
}
/// <summary>
/// Throws an <see cref="Win32Exception"/> if <paramref name="condition"/> is <see langword="true"/>.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="condition">The condition to decide whether to throw the exception.</param>
/// <param name="hresult">The error code to report.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIf(this Win32Exception? _, [DoesNotReturnIf(true)] bool condition, int hresult)
{
if (condition)
{
Throw(hresult);
}
}
/// <summary>
/// Throws an <see cref="Win32Exception"/> if a given error code represents an error.
/// </summary>
/// <param name="_">Dummy value to invoke the extension upon (always pass <see langword="null"/>.</param>
/// <param name="hresult">The error code to check.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfFailed(this Win32Exception? _, int hresult)
{
if (hresult < 0)
{
Throw(hresult);
}
}
/// <summary>
/// Throws an <see cref="Win32Exception"/> for a given error code.
/// </summary>
/// <param name="hresult">The error code to report.</param>
/// <exception cref="Win32Exception">Thrown with the given error code.</exception>
[DoesNotReturn]
private static void Throw(int hresult)
{
throw new Win32Exception(hresult);
}
}