序列组成的函数功能? [英] Serialize Composed Func?

查看:133
本文介绍了序列组成的函数功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这工作得很好:

    Func<string, string> func1 = s => s + "func";
    ViewState["function"] = func1;



不过,这并不:

However, this does not:

    Func<string, string> func1 = s => s + "func";
    Func<string, string> func2 = s => func1(s);

    ViewState["function"] = func2;



这引发运行时异常的序列化:键入MyProjectName._Default +<方式> c__DisplayClass3'在大会'MyProjectName,版本= 1.0.0.0,文化=中立,公钥=空'未标记为可序列

现在,我可以解决此这段时间,但我想明白为什么会发生这样的是,如果在未来,我别无选择,只能串行化之前撰写的功能,我将有一个解决方案。

Now, I can work around this this time, but I'd like to understand why this is happening so that if, in the future, I have no choice but to compose functions before serialization, I'll have a solution.

推荐答案

这是怎么回事在第二种情况下是封闭参与。 FUNC2 内使用 FUNC1 中创建的lambda表达式之间的封闭拍摄的共享状态。闭包是不可序列。当你尝试序列化尝试序列化目标对象是封闭的FUNC,你会得到你的异常。

What's happening in the second case is that a closure is involved. The use of func1 inside of func2 creates a closure to captured the shared state between the lambdas expressions. Closures are not serializable. When you try and serialize the func it tries to serialize the target object which is the closure and you get your exception.

这篇关于序列组成的函数功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆