[英]Issue with Code Contract in c#
I am facing a problem that I can not figure out. 我面临一个我无法解决的问题。
Say I have two methods: public void Method1(object obj)
in ViewModel
class and public void Method2(object obj)
in Model
class. 说我有两种方法:
public void Method1(object obj)
的ViewModel
类和public void Method2(object obj)
的Model
类。
Method2
gets called from Method1
using the instance of Model
class(say, objM is the object of Model
class and a member of ViewModel
class). Method2
Model
类的实例从Method1
调用Method1
(例如,objM是Model
类的对象,并且是ViewModel
类的成员)。
class ViewModel
{
public void Methods1(object obj)
{
if (!(
( (false == this.HasSal)
&& (typeof(Class1) == obj.GetType())
)
||
( (true == this.HasSal)
&& (typeof(Class2) == obj.GetType())
)
)
)
{
throw new ArgumentException("invalid obj");
}
Contract.EndContractBlock();
objM.Method2(obj);
.....
}
}
class Model
{
public void Method2(object obj)
{
Contract.Requires(
( (false == this.HasSal)
&& (typeof(Class1) == obj.GetType())
)
||
( (true == this.HasSal)
&& (typeof(Class2) == obj.GetType())
)
);
.....
}
}
Now whenever I try to build the code, Visual studio produces following warning 现在,每当我尝试构建代码时,Visual Studio都会产生以下警告
Code contracts: Requires unproven
(
( (false == this.HasSal)
&& (typeof(Class1) == obj.GetType())
)
||
( (true == this.HasSal)
&& (typeof(Class2) == obj.GetType())
)
)
Please suggest. 请提出建议。
As rtrokzzz have given link to another SO question .NET 4 Code Contracts: “requires unproven: source != null” in comments 由于rtrokzzz已给另一个SO问题.NET 4代码契约提供了链接:“需要未经证实的:来源!= null”在注释中
Solution for you is to add Contract.Ensures
in Method1()
and Method2()
您的解决方案是添加
Contract.Ensures
在Method1()
和Method1()
Method2()
Please note: I have not used Code Contracts
but I believe from my understanding that code will be as 请注意:我没有使用过
Code Contracts
但据我所知,我相信代码将
Contract.Ensures(obj != null);
Contract.Requires(obj != null);
Refer: How to avoid “source !=null” when using Code Contracts and Linq To Sql? 请参阅: 使用代码协定和Linq To Sql时如何避免“ source!= null”?
I don't believe that the static checker will ever be able to verify your contract, because the type of obj
isn't known until run-time - there is no guarantee that only objects of type Class1
or Class2
will be passed to Method1
. 我不认为静态检查器将能够验证您的合同,因为在运行时才知道
obj
的类型-无法保证仅将Class1
或Class2
类型的对象传递给Method1
。
It might be possible to prove this by adding additional contracts to methods that call Method1
. 通过向调用
Method1
方法添加其他协定,可以证明这Method1
。 If you include that code, I might be able to suggest a way to satisfy the static checker. 如果您包含该代码,我也许可以提出一种满足静态检查器要求的方法。
EDIT: Actually, there's another problem too. 编辑:其实,还有另一个问题。 If
HasSal
is a public-setter property, then I'm not sure your contract can be verified - there's always a possibility that another thread could change the value of HasSal
in between Method1
being called and the method body being executed. 如果
HasSal
是公开设置的属性,那么我不确定您的合同是否可以得到验证-在调用Method1
和执行方法主体之间,总是有另一个线程可以更改HasSal
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.