You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试时Assertions扩展抛异常跳至DoesNotReturn方法而非调用行问题

调试过程中,当断言抛出异常时,调试器会跳转到工具类内部的异常抛出代码行,而非停留在调用断言的业务代码行。

举个例子,调用代码如下:

Assertions.ThrowIfNullOrWhiteSpace(symbol, nameof(symbol), "Symbol cannot be null or whitespace.");

异常触发时,调试器不会停在上述调用代码行,而是跳转到工具类内的异常抛出方法中。

扩展代码

public static class Assertions
{
    public static void ThrowIfNull<T>(T? value, string paramName, string message = "Parameter cannot be null.") where T : class
    {
        if (value == null)
        {
            ThrowArgumentNullException(paramName, message);
        }
    }

    public static void ThrowIfNullOrEmpty(string value, string paramName, string message = "String cannot be empty.")
    {
        if (string.IsNullOrEmpty(value))
        {
            ThrowArgumentException(paramName, message);
        }
    }

    public static void ThrowIfNullOrEmpty<T>(ICollection<T>? collection, string paramName, string message = "Collection must have elements.")
    {
        if (collection == null || !collection.Any())
        {
            ThrowArgumentException(paramName, message);
        }
    }

    public static void ThrowIfNullOrEmpty<T>(T[]? array, string paramName, string message = "Array must have elements.")
    {
        if (array == null || array.Length == 0)
        {
            ThrowArgumentException(paramName, message);
        }
    }

    public static void ThrowIfNullOrWhiteSpace(string value, string paramName, string message = "String cannot be empty or whitespace.")
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            ThrowArgumentException(paramName, message);
        }
    }

    public static void ThrowIfOutOfRange<T>(T value, T minValue, T maxValue, string paramName, string message = "Value must be within the specified range.") where T : IComparable<T>
    {
        if (value.CompareTo(minValue) < 0 || value.CompareTo(maxValue) > 0)
        {
            ThrowArgumentOutOfRangeException(paramName, message);
        }
    }

    public static void ThrowIfLessThan<T>(T value, T threshold, string paramName, string message = "Value must be greater than or equal to the threshold.") where T : IComparable<T>
    {
        if (value.CompareTo(threshold) < 0)
        {
            ThrowArgumentOutOfRangeException(paramName, message);
        }
    }

    public static void ThrowIfLessThanOrEqualTo<T>(T value, T threshold, string paramName, string message = "Value must be greater than the threshold.") where T : IComparable<T>
    {
        if (value.CompareTo(threshold) <= 0)
        {
            ThrowArgumentOutOfRangeException(paramName, message);
        }
    }

    public static void ThrowIfGreaterThan<T>(T value, T threshold, string paramName, string message = "Value must be less than the threshold.") where T : IComparable<T>
    {
        if (value.CompareTo(threshold) > 0)
        {
            ThrowArgumentOutOfRangeException(paramName, message);
        }
    }

    public static void ThrowIfGreaterThanOrEqualTo<T>(T value, T threshold, string paramName, string message = "Value must be less than or equal to the threshold.") where T : IComparable<T>
    {
        if (value.CompareTo(threshold) >= 0)
        {
            ThrowArgumentOutOfRangeException(paramName, message);
        }
    }

    public static void ThrowIfEqual<T>(T value, T target, string paramName, string message = "Parameter cannot be equal to its default value.")
    {
        if (EqualityComparer<T>.Default.Equals(value, target))
        {
            ThrowArgumentException(paramName, message);
        }
    }

    [DoesNotReturn]
    private static void ThrowArgumentException(string paramName, string message) => throw new ArgumentException(message, paramName);

    [DoesNotReturn]
    private static void ThrowArgumentOutOfRangeException(string paramName, string message) => throw new ArgumentOutOfRangeException(paramName, message);

    [DoesNotReturn]
    private static void ThrowArgumentNullException(string paramName, string message) => throw new ArgumentNullException(paramName, message);
}

内容的提问来源于stack exchange,提问作者nop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 18:12:42