Menu

1 / 2

Decompiling C# expression trees: the lambda that became a tree of factory calls

DEV Community: csharp·Zero Heartbeat·about 14 hours ago
#NLHeuI2f
#devtocsharp#devto#dev#expression#lambda
Reading 0:00
15s threshold

A lambda is one of the few constructs in C# whose compilation depends on something outside the lambda. Write x => x + 1 and assign it to a Func<int, int> and you get a hidden static method and a delegate; assign the identical text to an Expression<Func<int, int>> and you get something else entirely — a nested sequence of calls to the factory methods on System.Linq.Expressions.Expression that builds a runtime data structure describing the lambda, node by node. There is no expression-tree opcode and no dedicated metadata. The decompiled method is a page of Expression.Parameter , Expression.Constant , Expression.Add and Expression.Lambda calls, with every member the source named reached through ldtoken .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.