调用重载方法时,代码合约会发出有关参数的警告
我有这段代码(非常精简):
[ContractClass(typeof (QueryServiceContract))]
public interface IQueryService
{
IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery);
IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class;
}
[ContractClassFor(typeof (IQueryService))]
public abstract class QueryServiceContract : IQueryService
{
public IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery)
{
Contract.Requires(multiIndexQuery != null);
Contract.Ensures(Contract.Result<IEnumerable<Document>>() != null);
return null;
}
public IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class
{
Contract.Requires(multiIndexQuery != null);
Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);
return null;
}
}
public class QueryService : IQueryService
{
public IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery)
{
throw new NotImplementedException();
}
public IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class
{
ExecuteQuery(multiIndexQuery); // This is the row code contracts complain about.
throw new NotImplementedException();
}
}
public class Document
{
}
public interface IMultiIndexQuery
{
}
代码合同给了我这个警告: CodeContracts: require unproven: multiIndexQuery != null
为什么我会收到该警告? QueryServiceContract 已为该参数不为空建立了契约。无论是在调用还是被调用的方法中......
I've this code (very stripped down):
[ContractClass(typeof (QueryServiceContract))]
public interface IQueryService
{
IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery);
IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class;
}
[ContractClassFor(typeof (IQueryService))]
public abstract class QueryServiceContract : IQueryService
{
public IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery)
{
Contract.Requires(multiIndexQuery != null);
Contract.Ensures(Contract.Result<IEnumerable<Document>>() != null);
return null;
}
public IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class
{
Contract.Requires(multiIndexQuery != null);
Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);
return null;
}
}
public class QueryService : IQueryService
{
public IEnumerable<Document> ExecuteQuery(IMultiIndexQuery multiIndexQuery)
{
throw new NotImplementedException();
}
public IEnumerable<T> ExecuteQuery<T>(IMultiIndexQuery multiIndexQuery) where T : class
{
ExecuteQuery(multiIndexQuery); // This is the row code contracts complain about.
throw new NotImplementedException();
}
}
public class Document
{
}
public interface IMultiIndexQuery
{
}
Code contracts give me this warning:
CodeContracts: requires unproven: multiIndexQuery != null
Why do I get that warning?
The QueryServiceContract has set up a contract for that parameter not being null. Both in calling and the called method...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在阅读这段代码时也迷失了......
你确定它不会进入无限递归吗?
作为测试,我重命名了 1 个 ExecuteQuery() 重载(在几个地方):
这满足了检查器的要求。
所以也许您发现了分析器的弱点,但在报告之前我会确保代码按预期工作。
I got lost while reading this code too...
Are you sure it isn't going into unbounded recursion?
As a test I renamed 1 of the ExecuteQuery() overloads (in several places):
And that satisfies the checker.
So maybe you found a weakness in the analyzer, but before reporting it I would make sure the code works as expected.