I understand that variable capturing is done by the compiler and not by the classes in the .NET framework itself. However, when the DLR was introduced, some of this work must surely have needed to have been done within the framework so as to defer it to runtime.
For e.g., in the piece of code given below:
dynamic d = ...
Func<int, bool> func = n => n > d;
The type resolution of the variable d
and its verification that it is an integer must have to be done at run-time. And since d
is a variable in the containing method of the lambda, it will be captured into a closure. This part will surely be done at run time.
Hence, I infer there must be some part of the DLR assemblies (System.Core.dll mostly) that does this part.
I've been searching and I could find some classes that look suspiciously reprehensible for this sort of a task. Specifically, ExpressionQuoter (despite the looks of it, this class does not quote lambda expressions like the Expression.Quote
method does), HoistedLocals, and the VariableBinder.
I thought I would invite someone who knows better to answer this.
Which class or part of the .NET framework turns locals that are in containing methods of lambdas (or anonymous methods) into those separate classes that have static variables representing them?