使用 IQueryable 投影到匿名类型时违反代码约定
我很难理解为什么我在以下代码片段上遇到代码契约违规:
// servicequery is of type System.Data.Services.Client.DataServiceQuery<DividendData>
// therefore implements IQueryable
var serviceQuery = CreateDataService().DividendData;
Contract.Assume(serviceQuery != null);
Contract.Assume(serviceQuery.Any());
var data = serviceQuery
.Select(div => new {I = 2});
违规行为是:CodeContracts:requires unproven:constructor != null
就在作业上。
如果我将此代码更改为以下内容,违规行为就会消失:
class resulttype
{
public int I { get; set; }
}
var serviceQuery = CreateDataService().DividendData;
Contract.Assume(serviceQuery != null);
Contract.Assume(serviceQuery.Any());
var data = serviceQuery
.Select(div => new resulttype() {I = 2});
我的问题是“为什么?”。
I'm having a hard time understanding why I am getting a code contract violation on the following piece of code:
// servicequery is of type System.Data.Services.Client.DataServiceQuery<DividendData>
// therefore implements IQueryable
var serviceQuery = CreateDataService().DividendData;
Contract.Assume(serviceQuery != null);
Contract.Assume(serviceQuery.Any());
var data = serviceQuery
.Select(div => new {I = 2});
The violation is: CodeContracts: requires unproven: constructor != null
right on the assignment.
If I change this code to the following, the violation goes away:
class resulttype
{
public int I { get; set; }
}
var serviceQuery = CreateDataService().DividendData;
Contract.Assume(serviceQuery != null);
Contract.Assume(serviceQuery.Any());
var data = serviceQuery
.Select(div => new resulttype() {I = 2});
My question is 'why?'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来它需要一个实际的课程作为合同。你能做的是:
无论如何,你在那里做的事情很奇怪......
It seems it need an actual class as a contract. What you can do is:
Anyway, it's very weird what you are doing there...