I'm experimenting with Code Contract and I encountered one problem. I have class:
我正在试验代码契约,遇到了一个问题。我有类:
public class SpecialPoint
{
public int X { get; set; }
public int Y { get; set; }
public SpecialPoint(int x, int y)
{
Contract.Requires<ArgumentException>(y > x);
X = x;
Y = y;
}
[ContractInvariantMethod]
private void ClassContract()
{
Contract.Invariant(Y > X);
}
}
and I run a test against it:
我对它做了一个测试
[TestFixture]
class SpecialPointTests
{
[Test]
public void SpecialPoint()
{
var p = new SpecialPoint(10, 20);
p.X = 30;
}
}
I expected static checker to warn me about assignment p.X =30; as this violates invariant but it only takes place during runtime. I have static analysis enable. My version is 1.7.11202.10.
我希望静态检查器能提醒我p任务。X = 30;因为这违反了不变性,但它只发生在运行时。我可以进行静态分析。我的版本是1.7.11202.10。
3
From the MSDN page on Contract.Invariant
来自合同上的MSDN页面
During run-time checking, invariants are checked at the end of each public method.
在运行时检查期间,在每个公共方法的末尾检查不变量。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/12/11/7813dd77d89564addc9fbe5848fa4d2f.html。