Updated Question Further Down
I\'ve been experimenting with expression trees in .NET 4 to generate code at runtime and I\'ve been trying to implement th
You problem is that you didn't pass parameters and variables to your block expression. You use them in the "inner" expressions, but the block expression knows nothing about them. Basically, all you need to do is to pass all your parameters and variables to a block expression.
var @foreach = Expression.Block(
new ParameterExpression[] { item, enumerator, param },
assignToEnum,
Expression.Loop(
Expression.IfThenElse(
Expression.NotEqual(doMoveNext, Expression.Constant(false)),
assignCurrent,
Expression.Break(@break))
, @break)
);