开发者

Is it possible to trick expression tree serialization into supporting recursion via a substitute token?

We all know that Expression Trees do not support recursion. I can create a recursive Func, wrap it in an expression tree, and invoke it.

Func<int, int> func1 = null;

func1 = x => (x == 0) ? 1 : x * func1(x - 1);

Expression<Func<int, int>> expression1 = i => func1(i);

var func1 = expression1.Compile();

var z1 = func1.Invoke(5);
开发者_开发知识库

However, when I try to serialize (eg using MetaLINQ) , I of course get an exception as it is a recursive expression.

Lisp constructs expression trees and supports recursion. Genetic Programming mutates expression trees. There are some types of problems whose solutions are inherently recursive, because of prior state they need to track: tree traversal, depth-first search, and divide-and-conquer algorithms. I am aiming to generate efficient algorithms within a managed distributed environment, where branches can be passed around and recombined.

My question is: How could I override the serialization / deserialization to swap out / in the call with a non recursive "mock" token , swapping the recursive call back in when reloading the deserialized tree?


I know this is not an exact answer to your question, but I would apply the Replace Recursion with Iteration Refactoring pattern.

Recursion is usually considered bad, and sometime looked upon as lazy. I however understand the need to solve the problem first, then refactor the code so that is can be maintained and read with ease.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜