对于Command对象Command
,我有一个私有执行方法ClickExecute
。我绑定了一个类型为TypeA
的参数对象。它有一个财产IsValid
,我想检查在代码合同如下所示。专用执行方法CodeContract错误CC1025
ClickCommand = new DelegatingCommand(ClickExecute);
private void ClickExecute(object parameter)
{
var typeA= parameter as TypeA;
Contract.Requires<ArgumentNullException>(typeA!= null, "typeA");
Contract.Requires<ArgumentException>(typeA.IsValid, "Not Valid");
}
当我这样做,我得到的编译错误 - error CC1025: After contract block, found use of local variable 'typeA' defined in contract block
我在做什么错?我需要做一个类型转换我检查之前
[编辑]
马修的答案是有帮助和解决CodeContract问题是 -
Contract.Requires<ArgumentNullException>(parameter is TypeA);
Contract.Requires<ArgumentException>((parameter as TypeA).IsValid);
var typeA = parameter as TypeA;
但这导致重复类型的新问题用这种方法铸造并导致静态代码分析错误 -
CA1800 : Microsoft.Performance : 'parameter', a parameter, is cast to type 'TypeA' multiple times in method
有没有更好的方法来避免这种情况?
道歉的错误,第二份合同应该是TypeA.IsValid – CarbineCoder
@Bharath我相应地更新了我的答案。 –
谢谢马修,这非常有用,并解决了Codecontract问题。从你的回答我喜欢 - 'Contract.Requires((参数为typeA).IsValid);'比显式类型转换。老习惯:) –
CarbineCoder