Use ThrowHelper within ArgHelper

This commit is contained in:
Dustin Campbell 2024-07-18 09:39:58 -07:00
Родитель bdb1348608
Коммит 3b480de903
7 изменённых файлов: 279 добавлений и 178 удалений

Просмотреть файл

@ -19,7 +19,7 @@ internal static class ArgHelper
#else
if (argument is null)
{
ThrowArgumentNullException(paramName);
ThrowHelper.ThrowArgumentNullException(paramName);
}
#endif
}
@ -31,36 +31,20 @@ internal static class ArgHelper
#else
if (argument is null)
{
ThrowArgumentNullException(paramName);
ThrowHelper.ThrowArgumentNullException(paramName);
}
#endif
}
#if !NET8_0_OR_GREATER
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string? paramName)
{
throw new ArgumentNullException(paramName);
}
#endif
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET8_0_OR_GREATER
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
#else
if (argument.IsNullOrEmpty())
{
ThrowException(argument, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.The_value_cannot_be_an_empty_string, paramName);
ThrowHelper.ThrowArgumentException(paramName, SR.The_value_cannot_be_an_empty_string);
}
#endif
}
@ -72,16 +56,9 @@ internal static class ArgHelper
#else
if (argument.IsNullOrWhiteSpace())
{
ThrowException(argument, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException(SR.The_value_cannot_be_an_empty_string_composed_entirely_of_whitespace, paramName);
ThrowHelper.ThrowArgumentException(paramName, SR.The_value_cannot_be_an_empty_string_composed_entirely_of_whitespace);
}
#endif
}
@ -93,14 +70,7 @@ internal static class ArgHelper
#else
if (value == 0)
{
ThrowException(value, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_zero_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_zero_value(paramName, value));
}
#endif
}
@ -112,14 +82,7 @@ internal static class ArgHelper
#else
if (value < 0)
{
ThrowException(value, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_value(paramName, value));
}
#endif
}
@ -131,14 +94,7 @@ internal static class ArgHelper
#else
if (value <= 0)
{
ThrowException(value, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(int value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_and_non_zero_value(paramName, value));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_a_non_negative_and_non_zero_value(paramName, value));
}
#endif
}
@ -151,14 +107,7 @@ internal static class ArgHelper
#else
if (EqualityComparer<T>.Default.Equals(value, other))
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_not_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_not_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
}
#endif
}
@ -171,14 +120,7 @@ internal static class ArgHelper
#else
if (!EqualityComparer<T>.Default.Equals(value, other))
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_equal_to_2(paramName, (object?)value ?? "null", (object?)other ?? "null"));
}
#endif
}
@ -191,14 +133,7 @@ internal static class ArgHelper
#else
if (value.CompareTo(other) > 0)
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_or_equal_to_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_or_equal_to_2(paramName, value, other));
}
#endif
}
@ -211,14 +146,7 @@ internal static class ArgHelper
#else
if (value.CompareTo(other) >= 0)
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_less_than_2(paramName, value, other));
}
#endif
}
@ -231,14 +159,7 @@ internal static class ArgHelper
#else
if (value.CompareTo(other) < 0)
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_or_equal_to_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_or_equal_to_2(paramName, value, other));
}
#endif
}
@ -251,14 +172,7 @@ internal static class ArgHelper
#else
if (value.CompareTo(other) <= 0)
{
ThrowException(value, other, paramName);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowException(T value, T other, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_2(paramName, value, other));
ThrowHelper.ThrowArgumentOutOfRangeException(paramName, value, SR.Format0_1_must_be_greater_than_2(paramName, value, other));
}
#endif
}

Просмотреть файл

@ -4,10 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
#if !NET
using ThrowHelper = Microsoft.AspNetCore.Razor.Utilities.ThrowHelper;
#endif
using Microsoft.AspNetCore.Razor;
namespace Microsoft.AspNetCore.Razor;
@ -43,7 +40,7 @@ internal static class ArrayExtensions
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}
return default;
@ -86,7 +83,7 @@ internal static class ArrayExtensions
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(array));
ThrowHelper.ThrowArgumentNullException(nameof(array));
}
return default;
@ -128,7 +125,7 @@ internal static class ArrayExtensions
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}
return default;
@ -172,7 +169,7 @@ internal static class ArrayExtensions
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(array));
ThrowHelper.ThrowArgumentNullException(nameof(array));
}
return default;

Просмотреть файл

@ -7,6 +7,7 @@ using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Utilities;
namespace Microsoft.AspNetCore.Razor.PooledObjects;
@ -1306,7 +1307,7 @@ internal partial struct PooledArrayBuilder<T> : IDisposable
/// </summary>
[DoesNotReturn]
private static T ThrowInvalidOperation(string message)
=> ThrowHelper.ThrowInvalidOperation<T>(message);
=> ThrowHelper.ThrowInvalidOperationException<T>(message);
[MemberNotNull(nameof(_builder))]
private void MoveInlineItemsToBuilder()

Просмотреть файл

@ -2,8 +2,8 @@
// Licensed under the MIT license. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Utilities;
namespace System.Collections.Generic;
@ -169,7 +169,7 @@ internal static class ReadOnlyListExtensions
/// The list is empty.
/// </exception>
public static T First<T>(this IReadOnlyList<T> list)
=> list.Count > 0 ? list[0] : ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_elements);
=> list.Count > 0 ? list[0] : ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_elements);
/// <summary>
/// Returns the first element in a list that satisfies a specified condition.
@ -196,7 +196,7 @@ internal static class ReadOnlyListExtensions
}
}
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
/// <summary>
@ -227,7 +227,7 @@ internal static class ReadOnlyListExtensions
}
}
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
/// <summary>
@ -329,7 +329,7 @@ internal static class ReadOnlyListExtensions
/// The list is empty.
/// </exception>
public static T Last<T>(this IReadOnlyList<T> list)
=> list.Count > 0 ? list[^1] : ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_elements);
=> list.Count > 0 ? list[^1] : ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_elements);
/// <summary>
/// Returns the last element of a list that satisfies a specified condition.
@ -356,7 +356,7 @@ internal static class ReadOnlyListExtensions
}
}
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
/// <summary>
@ -387,7 +387,7 @@ internal static class ReadOnlyListExtensions
}
}
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
/// <summary>
@ -559,8 +559,8 @@ internal static class ReadOnlyListExtensions
return list.Count switch
{
1 => list[0],
0 => ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_elements),
_ => ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_element)
0 => ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_elements),
_ => ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_element)
};
}
@ -594,7 +594,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;
@ -604,7 +604,7 @@ internal static class ReadOnlyListExtensions
if (!firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
return result!;
@ -643,7 +643,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;
@ -653,7 +653,7 @@ internal static class ReadOnlyListExtensions
if (!firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_no_matching_elements);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_no_matching_elements);
}
return result!;
@ -679,7 +679,7 @@ internal static class ReadOnlyListExtensions
{
1 => list[0],
0 => default,
_ => ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_element)
_ => ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_element)
};
}
@ -706,7 +706,7 @@ internal static class ReadOnlyListExtensions
{
1 => list[0],
0 => defaultValue,
_ => ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_element)
_ => ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_element)
};
}
@ -738,7 +738,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;
@ -780,7 +780,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;
@ -822,7 +822,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;
@ -867,7 +867,7 @@ internal static class ReadOnlyListExtensions
{
if (firstSeen)
{
return ThrowHelper.ThrowInvalidOperation<T>(SR.Contains_more_than_one_matching_element);
return ThrowHelper.ThrowInvalidOperationException<T>(SR.Contains_more_than_one_matching_element);
}
firstSeen = true;

Просмотреть файл

@ -4,8 +4,7 @@
using System.Diagnostics.CodeAnalysis;
#if !NET
using ArgHelper = Microsoft.AspNetCore.Razor.ArgHelper;
using ThrowHelper = Microsoft.AspNetCore.Razor.Utilities.ThrowHelper;
using Microsoft.AspNetCore.Razor;
#endif
namespace System;
@ -73,7 +72,7 @@ internal static class StringExtensions
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}
return default;
@ -111,7 +110,7 @@ internal static class StringExtensions
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(text));
ThrowHelper.ThrowArgumentNullException(nameof(text));
}
return default;
@ -251,7 +250,7 @@ internal static class StringExtensions
{
if (!startIndex.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentOutOfRange(nameof(startIndex));
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(startIndex));
}
return default;
@ -289,7 +288,7 @@ internal static class StringExtensions
{
if (!range.Start.Equals(Index.Start) || !range.End.Equals(Index.Start))
{
ThrowHelper.ThrowArgumentNull(nameof(text));
ThrowHelper.ThrowArgumentNullException(nameof(text));
}
return default;

Просмотреть файл

@ -0,0 +1,235 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Microsoft.AspNetCore.Razor;
internal static class ThrowHelper
{
/// <summary>
/// Throws an <see cref="ArgumentException"/> with a parameter name and a message.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentException(string? paramName, string message)
=> throw new ArgumentException(message, paramName);
/// <summary>
/// Throws an <see cref="ArgumentException"/> with a parameter name and a message.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowArgumentException<T>(string? paramName, string message)
=> throw new ArgumentException(message, paramName);
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> with a parameter name.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentNullException(string? paramName)
=> throw new ArgumentNullException(paramName);
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> with a parameter name.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowArgumentNullException<T>(string? paramName)
=> throw new ArgumentNullException(paramName);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name, message, and
/// the actual invalid value.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="actualValue">
/// The actual invalid value to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeException(string? paramName, object? actualValue, string message)
=> throw new ArgumentOutOfRangeException(paramName, actualValue, message);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name and message.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeException(string? paramName, string message)
=> throw new ArgumentOutOfRangeException(paramName, message);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowArgumentOutOfRangeException(string? paramName)
=> throw new ArgumentOutOfRangeException(paramName);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name, message, and
/// the actual invalid value.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="actualValue">
/// The actual invalid value to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowArgumentOutOfRangeException<T>(string? paramName, object? actualValue, string message)
=> throw new ArgumentOutOfRangeException(paramName, actualValue, message);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name and message.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowArgumentOutOfRangeException<T>(string? paramName, string message)
=> throw new ArgumentOutOfRangeException(paramName, message);
/// <summary>
/// Throws an <see cref="ArgumentOutOfRangeException"/> with a parameter name.
/// </summary>
/// <param name="paramName">
/// The parameter name to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowArgumentOutOfRangeException<T>(string? paramName)
=> throw new ArgumentOutOfRangeException(paramName);
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> with a message.
/// </summary>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowInvalidOperationException(string message)
=> throw new InvalidOperationException(message);
/// <summary>
/// Throws an <see cref="InvalidOperationException"/> with a message.
/// </summary>
/// <param name="message">
/// The message to include in the exception.
/// </param>
/// <returns>
/// This method does not return because it always throws an exception, but it is defined to return a
/// <typeparamref name="T"/> value. This is useful for control flow scenarios where it is necessary to
/// throw an exception and return from a method.
/// </returns>
/// <remarks>
/// This helps the JIT inline methods that need to throw an exceptions.
/// </remarks>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static T ThrowInvalidOperationException<T>(string message)
=> throw new InvalidOperationException(message);
}

Просмотреть файл

@ -1,45 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.AspNetCore.Razor.Utilities;
internal static class ThrowHelper
{
/// <summary>
/// This is present to help the JIT inline methods that need to throw an <see cref="InvalidOperationException"/>.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentNull(string paramName)
=> throw new ArgumentNullException(paramName);
/// <summary>
/// This is present to help the JIT inline methods that need to throw an <see cref="InvalidOperationException"/>.
/// </summary>
[DoesNotReturn]
public static T ThrowArgumentNull<T>(string paramName)
=> throw new ArgumentNullException(paramName);
/// <summary>
/// This is present to help the JIT inline methods that need to throw an <see cref="InvalidOperationException"/>.
/// </summary>
[DoesNotReturn]
public static void ThrowArgumentOutOfRange(string paramName)
=> throw new ArgumentOutOfRangeException(paramName);
/// <summary>
/// This is present to help the JIT inline methods that need to throw an <see cref="InvalidOperationException"/>.
/// </summary>
[DoesNotReturn]
public static T ThrowArgumentOutOfRange<T>(string paramName)
=> throw new ArgumentOutOfRangeException(paramName);
/// <summary>
/// This is present to help the JIT inline methods that need to throw an <see cref="InvalidOperationException"/>.
/// </summary>
[DoesNotReturn]
public static T ThrowInvalidOperation<T>(string message)
=> throw new InvalidOperationException(message);
}