Ejemplo n.º 1
0
        public static Expression ApplyBinder(DynamicMetaObjectBinder binder, bool throwException, params Expression[] expressions)
        {
            var result = binder.Bind(DynamicMetaObject.Create(null, expressions[0]),
                                     expressions.Skip(1).Select(e =>
                                                                DynamicMetaObject.Create(null, e)).ToArray()
                                     );

            if (result.Expression.NodeType == ExpressionType.Convert)
            {
                var convert = (UnaryExpression)result.Expression;
                return(convert.Operand);
            }
            if (result.Expression.NodeType == ExpressionType.Throw)
            {
                if (throwException)
                {
                    // throw the exception
                    Expression.Lambda(result.Expression).Compile().DynamicInvoke();
                }
                else
                {
                    return(null);
                }
            }
            return(result.Expression);
        }
Ejemplo n.º 2
0
            public T /*!*/ Bind(DynamicMetaObjectBinder /*!*/ binder, int compilationThreshold, object[] args)
            {
                if (CachedBindingInfo <T> .LastInterpretedFailure != null && CachedBindingInfo <T> .LastInterpretedFailure.Binder == binder)
                {
                    // we failed the rule because we have a compiled target available, return the compiled target
                    Debug.Assert(CachedBindingInfo <T> .LastInterpretedFailure.CompiledTarget != null);
                    var res = CachedBindingInfo <T> .LastInterpretedFailure.CompiledTarget;
                    CachedBindingInfo <T> .LastInterpretedFailure = null;
                    return(res);
                }

                // we haven't produced a rule yet....
                var bindingInfo = new CachedBindingInfo <T>(binder, compilationThreshold);

                var targetMO = DynamicMetaObject.Create(args[0], _parameters[1]); // 1 is skipping CallSite

                DynamicMetaObject[] argsMO = new DynamicMetaObject[args.Length - 1];
                for (int i = 0; i < argsMO.Length; i++)
                {
                    argsMO[i] = DynamicMetaObject.Create(args[i + 1], _parameters[i + 2]);
                }
                var binding = binder.Bind(targetMO, argsMO);

                return(CreateDelegate(binding, bindingInfo));
            }
Ejemplo n.º 3
0
        public static LambdaExpression /*!*/ Bind <T>(this DynamicMetaObjectBinder binder, object[] args) where T : class
        {
            var signature = LambdaSignature <T> .Instance;

            LabelTarget returnLabel;

            if (signature.ReturnLabel.Type == typeof(object) && binder.ReturnType != typeof(void) && binder.ReturnType != typeof(object))
            {
                returnLabel = Expression.Label(binder.ReturnType);
            }
            else
            {
                returnLabel = signature.ReturnLabel;
            }

            Expression binding = binder.Bind(args, signature.Parameters, returnLabel);

            if (binding == null)
            {
                throw new InvalidOperationException("CallSiteBinder.Bind must return non-null meta-object");
            }

            return(Stitch <T>(binding, signature, returnLabel));
        }
Ejemplo n.º 4
0
 public static DynamicMetaObject Bind(DynamicMetaObjectBinder binder, object target, object[] args)
 {
     return(binder.Bind(CreateDynamicTarget(target), CreateDynamicArgs(args)));
 }