public virtual Statement BuildClosureForEach(Expression source, ref Expression target, out Block body, BlockScope scope) { source = this.GetInnerExpression(source); if (source is QueryExpression) { QueryYielder yielder = new QueryYielder(); yielder.Source = source; yielder.Body = body = new Block(new StatementList(1)); if (target == null) target = this.NewClosureLocal(this.typeSystem.GetStreamElementType(source, this.TypeViewer), scope); yielder.Target = target; yielder.State = this.NewClosureLocal(SystemTypes.Int32, scope); return yielder; }else{ if (target == null){ TypeNode elementType = this.typeSystem.GetStreamElementType(source, this.TypeViewer); target = target = this.NewClosureLocal(elementType, scope); } Literal tmpSource = new Literal(null, source.Type); ForEach fe = new ForEach(target.Type, target, tmpSource, new Block(new StatementList(1))); fe.ScopeForTemporaryVariables = scope; Checker checker = new Checker(null, this.typeSystem, null, null, null); checker.currentType = this.typeSystem.currentType = (scope.CapturedForClosure ? scope.ClosureClass : this.currentMethod.DeclaringType); checker.VisitForEach(fe); Debug.Assert(fe.SourceEnumerable != null); if (fe.SourceEnumerable != null){ Debug.Assert(fe.SourceEnumerable is CollectionEnumerator, "SourceEnumerable == "+fe.SourceEnumerable.GetType()); CollectionEnumerator ce = (CollectionEnumerator)fe.SourceEnumerable; ce.Collection = source; } body = fe.Body; return fe; } }