为什么序列化 DataContract 时不能使用 lambda?
下面制作了一些模拟代码来说明我的示例。问题在于 lambda 表达式。如果我将其保留为代码示例中的样子,那么当我尝试调用该服务时,它将不会序列化。 但是如果我在 lambda 后面输入 .ToList()
,它会按预期进行序列化。
这是为什么?我不明白为什么下面的代码不应该工作...... 有人愿意启发我吗? :)
var list = new EntityPerson
{
Names = modelPerson.Names.Select(
n => new EntityName
{
Text = n.Text
})
}
Made som mock code below to illustrate my example. The problem is the lambda expression. If I leave it as in the code example it will not serialize when I try to call the service. However if I type .ToList()
after the lambda it serializes as it should.
Why is that? I can't see why the code below should not work...
Anyone care to enlighten me? :)
var list = new EntityPerson
{
Names = modelPerson.Names.Select(
n => new EntityName
{
Text = n.Text
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为延迟执行。您不存储 lambda 执行的结果,而是存储表达式树或 lambda 本身,这需要序列化对 modelPerson 的引用 (!)。
http://blogs.msdn.com/b /charlie/archive/2007/12/09/deferred-execution.aspx 以及更多内容显示了与此相关的“问题”。 (谷歌搜索“延迟执行.net”了解更多信息。)
That's because of the deferred execution. You're not storing the result of the lambda execution, but rather the expression tree or lambda itself, which would need to serialize a reference (!) to the
modelPerson
.http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx and many more show the "problems" associated with this. (Google for "deferred execution .net" for more.)