2

我正在使用 Resharper 5.x 进行编译时分析,它通常非常好,但它似乎没有将代码契约应用于其逻辑。我有类似以下的内容,但我在标记线上遇到了问题。

public void Method(int arg)
{
    Contract.Requires(this.NullableValueType != null);

    this.Method2(
        arg,
        this.NullableValueType.Value, // [1]
        this.ReferenceType);
}

[1] 最终以“可能的'System.InvalidOperationException'”突出显示。有没有办法在不关闭检查的情况下摆脱这个错误?

4

1 回答 1

0

诚然,Resharper 可以更智能并考虑合同,但不幸的是目前还没有。

我建议使该行更明确。代替

this.NullableValueType.Value

你可以写

this.NullableValueType ?? <something>

当然,“某事”是无关紧要的,因为它永远不会发生(例如,new ThatValueType())。

于 2010-09-15T21:58:32.647 回答