C#:代码契约与普通参数验证

标签 c# .net .net-4.0 code-contracts microsoft-contracts

考虑以下两段代码:

    public static Time Parse(string value)
    {
        string regXExpres = 
           "^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$";
        Contract.Requires(value != null);
        Contract.Requires(new Regex(regXExpres).IsMatch(value));
        string[] tokens = value.Split(':');
        int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
        int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
        return new Time(hour, minute);
    }

    public static Time Parse(string value)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
        string[] tokens = value.Split(':');
        if (tokens.Length != 2)
        {
            throw new FormatException("value must be h:m");
        }
        int hour = Convert.ToInt32(tokens[0], CultureInfo.InvariantCulture);
        if (!(0 <= hour && hour <= 24))
        {
            throw new FormatException("hour must be between 0 and 24");
        }
        int minute = Convert.ToInt32(tokens[1], CultureInfo.InvariantCulture);
        if (!(0 <= minute && minute <= 59))
        {
            throw new FormatException("minute must be between 0 and 59");
        }
        return new Time(hour, minute);
    }

我个人更喜欢第一个版本,因为代码更清晰、更小,而且可以轻松关闭 Contracts。但缺点是 Visual Studio Code Analysis 指责我应该检查参数值是否为 null,而构造函数的 Contracts 没有意识到正则表达式确保分钟和小时在给定范围内。

所以我最终得到了很多错误的警告,而且除了 RegEx 验证之外,我看不出有什么方法可以在不最终抛出 FormatExceptions 的情况下验证带有契约(Contract)的字符串值。

对于如何使用代码合约解决这个问题和同等情况有什么建议吗?

最佳答案

为了摆脱警告,您可以使用 Contract.Assume

关于C#:代码契约与普通参数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1980689/

相关文章:

c# - 使用Parallel Foreach和WhenAny的多个任务。那可能吗?

.net - 在 WinForms 中使用自定义 WPF 控件

com - .NET 是 COM 的替代品吗?

c# - PureAttribute 对参数的用途

c# - Visual Studio 2010 中的 json 支持

c# - 如何在 C# 部署中包含文件?

c# - 如何保存 BitmapImage WinRT

c# - 非字符串引用类型的 const 字段只能用 null 错误初始化

c# - .NET 中的 protected 类

c# - Code Contracts - 不错,处于边缘,但还没有准备好迎接黄金时段?