Mono.CSharp.ResolveContext.MustCaptureVariable C# (CSharp) Method

MustCaptureVariable() public method

public MustCaptureVariable ( INamedBlockVariable local ) : bool
local INamedBlockVariable
return bool
		public bool MustCaptureVariable (INamedBlockVariable local)
		{
			if (CurrentAnonymousMethod == null)
				return false;

			// FIXME: IsIterator is too aggressive, we should capture only if child
			// block contains yield
			if (CurrentAnonymousMethod.IsIterator)
				return true;

			return local.Block.ParametersBlock != CurrentBlock.ParametersBlock.Original;
		}

Usage Example

		Expression DoResolveBase (ResolveContext ec)
		{
			type = local_info.VariableType;

			Expression e = Block.GetConstantExpression (Name);
			if (e != null)
				return e.Resolve (ec);

			VerifyAssigned (ec);

			//
			// If we are referencing a variable from the external block
			// flag it for capturing
			//
			if (ec.MustCaptureVariable (local_info)) {
				if (local_info.AddressTaken)
					AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, this, loc);

				if (ec.IsVariableCapturingRequired) {
					AnonymousMethodStorey storey = local_info.Block.Explicit.CreateAnonymousMethodStorey (ec);
					storey.CaptureLocalVariable (ec, local_info);
				}
			}

			resolved |= ec.DoFlowAnalysis;
			eclass = ExprClass.Variable;
			return this;
		}