我正在做一个简单的代码合同测试。下面的代码是在一个winform中。这传递(当然):C#代码合同:为什么不能证明这个简单的条件?
private void Test(Form form)
{
Contract.Requires(!string.IsNullOrEmpty(form.Name));
MessageBox.Show(form.Name);
}
protected override void OnLoad(EventArgs e)
{
if (!string.IsNullOrEmpty(Name))
Test(this);
base.OnLoad(e);
}
不过,我增加一个间接的只是一个很简单的层面,它说:“需要得到证实”:
private bool Valid(string str)
{
return !string.IsNullOrEmpty(str);
}
protected override void OnLoad(EventArgs e)
{
if (Valid(Name))
Test(this);
base.OnLoad(e);
}
这似乎将是微不足道的证明。为什么它不工作?
嗯,好的。这看起来有点用处不大,但我会接受你的话。 – 2010-08-18 23:38:29