Example #1
0
        public override DynamicMetaObject Bind(DynamicMetaObject target, params DynamicMetaObject[] args)
        {
            args = ArrayUtils.Insert(target, args);

            List <DynamicMetaObject>   results      = new List <DynamicMetaObject>(_metaBinders.Length);
            List <Expression>          steps        = new List <Expression>();
            List <ParameterExpression> temps        = new List <ParameterExpression>();
            BindingRestrictions        restrictions = BindingRestrictions.Empty;

            for (int i = 0; i < _metaBinders.Length; i++)
            {
                BinderMappingInfo curBinder = _metaBinders[i];

                DynamicMetaObject[] tmpargs = GetArguments(args, results, i);
                DynamicMetaObject   next    = curBinder.Binder.Bind(tmpargs[0], ArrayUtils.RemoveFirst(tmpargs));
                if (i != 0)
                {
                    // If the rule contains an embedded "update", replace it with a defer
                    var visitor = new ReplaceUpdateVisitor {
                        Binder = curBinder.Binder, Arguments = tmpargs
                    };
                    next = new DynamicMetaObject(visitor.Visit(next.Expression), next.Restrictions);
                }

                restrictions = restrictions.Merge(next.Restrictions);
                if (next.Expression.NodeType == ExpressionType.Throw)
                {
                    // end of the line... the expression is throwing, none of the other
                    // binders will have an opportunity to run.
                    steps.Add(next.Expression);
                    break;
                }

                ParameterExpression tmp = Expression.Variable(next.Expression.Type, "comboTemp" + i.ToString());
                temps.Add(tmp);

                steps.Add(Expression.Assign(tmp, next.Expression));
                results.Add(new DynamicMetaObject(tmp, next.Restrictions));
            }

            return(new DynamicMetaObject(
                       Expression.Block(
                           temps.ToArray(),
                           steps.ToArray()
                           ),
                       restrictions
                       ));
        }
Example #2
0
        public override DynamicMetaObject Bind(DynamicMetaObject target, params DynamicMetaObject[] args) {
            args = ArrayUtils.Insert(target, args);

            List<DynamicMetaObject> results = new List<DynamicMetaObject>(_metaBinders.Length);
            List<Expression> steps = new List<Expression>();
            List<ParameterExpression> temps = new List<ParameterExpression>();
            BindingRestrictions restrictions = BindingRestrictions.Empty;

            for (int i = 0; i < _metaBinders.Length; i++) {
                BinderMappingInfo curBinder = _metaBinders[i];

                DynamicMetaObject[] tmpargs = GetArguments(args, results, i);
                DynamicMetaObject next = curBinder.Binder.Bind(tmpargs[0], ArrayUtils.RemoveFirst(tmpargs));
                if (i != 0) {
                    // If the rule contains an embedded "update", replace it with a defer
                    var visitor = new ReplaceUpdateVisitor { Binder = curBinder.Binder, Arguments = tmpargs };
                    next = new DynamicMetaObject(visitor.Visit(next.Expression), next.Restrictions);
                }

                restrictions = restrictions.Merge(next.Restrictions);
                if (next.Expression.NodeType == ExpressionType.Throw) {
                    // end of the line... the expression is throwing, none of the other 
                    // binders will have an opportunity to run.
                    steps.Add(next.Expression);
                    break;
                }

                ParameterExpression tmp = Expression.Variable(next.Expression.Type, "comboTemp" + i.ToString());
                temps.Add(tmp);

                steps.Add(Expression.Assign(tmp, next.Expression));
                results.Add(new DynamicMetaObject(tmp, next.Restrictions));
            }

            return new DynamicMetaObject(
                Expression.Block(
                    temps.ToArray(),
                    steps.ToArray()
                ),
                restrictions
            );
        }