private string[] GetCapturedLoopVariablesNames() { var capturedVariables = LambdaBlock.GetCapturedLoopVariables(this.Emitter, this.Context, this.Parameters); if (capturedVariables == null) { return(null); } List <string> names = new List <string>(); foreach (var capturedVariable in capturedVariables) { if (this.Emitter.LocalsMap != null && this.Emitter.LocalsMap.ContainsKey(capturedVariable)) { names.Add(this.RemoveReferencePart(this.Emitter.LocalsMap[capturedVariable])); } else if (this.Emitter.LocalsNamesMap != null && this.Emitter.LocalsNamesMap.ContainsKey(capturedVariable.Name)) { names.Add(this.RemoveReferencePart(this.Emitter.LocalsNamesMap[capturedVariable.Name])); } else { names.Add(capturedVariable.Name); } } return(names.ToArray()); }
public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression) { var capturedVariables = LambdaBlock.GetCapturedLoopVariables(this.Emitter, anonymousMethodExpression, anonymousMethodExpression.Parameters, true); if (capturedVariables != null) { DirectionVariables.AddRange(capturedVariables); } base.VisitAnonymousMethodExpression(anonymousMethodExpression); }
public override void VisitLambdaExpression(LambdaExpression lambdaExpression) { var capturedVariables = LambdaBlock.GetCapturedLoopVariables(this.Emitter, lambdaExpression, lambdaExpression.Parameters, true); if (capturedVariables != null) { DirectionVariables.AddRange(capturedVariables); } base.VisitLambdaExpression(lambdaExpression); }
internal static IVariable[] GetCapturedLoopVariables(IEmitter emitter, AstNode context, IEnumerable <ParameterDeclaration> parameters, bool excludeReadOnly = false) { var loop = LambdaBlock.GetOuterLoop(context); if (loop == null) { return(null); } var loopVariablesAnalyzer = new LoopVariablesAnalyzer(emitter, excludeReadOnly); loopVariablesAnalyzer.Analyze(loop); var captureAnalyzer = new CaptureAnalyzer(emitter); captureAnalyzer.Analyze(context, parameters.Select(p => p.Name)); return(captureAnalyzer.UsedVariables.Where(v => loopVariablesAnalyzer.Variables.Contains(v)).ToArray()); }