NewArrayHelper() публичный статический Метод

public static NewArrayHelper ( Type type, IEnumerable initializers ) : NewArrayExpression
type Type
initializers IEnumerable
Результат NewArrayExpression
        internal protected override Expression ToExpression(OverloadResolver resolver, RestrictedArguments args, bool[] hasBeenUsed)
        {
            Type dictType = ParameterInfo.ParameterType;

            return(Ast.Call(
                       GetCreationDelegate(dictType).Method,
                       Ast.NewArrayInit(typeof(string), ConstantNames()),
                       AstUtils.NewArrayHelper(typeof(object), GetParameters(args, hasBeenUsed))
                       ));
        }
Пример #2
0
        internal override DynamicMetaObject Call(OverloadResolverFactory resolverFactory, ActionBinder binder, params DynamicMetaObject[] arguments)
        {
            if (Method.IsPublic && Method.DeclaringType.IsVisible())
            {
                return(binder.MakeCallExpression(resolverFactory, Method, arguments));
            }

            //methodInfo.Invoke(obj, object[] params)
            if (Method.IsStatic)
            {
                return(new DynamicMetaObject(
                           Ast.Convert(
                               Ast.Call(
                                   AstUtils.Constant(Method),
                                   typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }),
                                   AstUtils.Constant(null),
                                   AstUtils.NewArrayHelper(typeof(object), ArrayUtils.ConvertAll(arguments, x => x.Expression))
                                   ),
                               Method.ReturnType
                               ),
                           BindingRestrictions.Empty
                           )
                       );
            }

            if (arguments.Length == 0)
            {
                throw Error.NoInstanceForCall();
            }

            return(new DynamicMetaObject(
                       Ast.Convert(
                           Ast.Call(
                               AstUtils.Constant(Method),
                               typeof(MethodInfo).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }),
                               arguments[0].Expression,
                               AstUtils.NewArrayHelper(typeof(object), ArrayUtils.ConvertAll(ArrayUtils.RemoveFirst(arguments), x => x.Expression))
                               ),
                           Method.ReturnType
                           ),
                       BindingRestrictions.Empty
                       ));
        }
Пример #3
0
        internal override Expression ToExpression(OverloadResolver resolver, IList <ArgBuilder> builders, RestrictedArguments args, Expression ret)
        {
            if (_returnArgs.Count == 1)
            {
                if (_returnArgs[0] == -1)
                {
                    return(ret);
                }
                return(Ast.Block(ret, builders[_returnArgs[0]].ToReturnExpression(resolver)));
            }

            Expression[] retValues = new Expression[_returnArgs.Count];
            int          rIndex    = 0;
            bool         usesRet   = false;

            foreach (int index in _returnArgs)
            {
                if (index == -1)
                {
                    usesRet             = true;
                    retValues[rIndex++] = ret;
                }
                else
                {
                    retValues[rIndex++] = builders[index].ToReturnExpression(resolver);
                }
            }

            Expression retArray = AstUtils.NewArrayHelper(typeof(object), retValues);

            if (!usesRet)
            {
                retArray = Ast.Block(ret, retArray);
            }

            return(resolver.GetByRefArrayExpression(retArray));
        }
Пример #4
0
        internal Expression MakeExpression(RestrictedArguments restrictedArgs)
        {
            bool[]       usageMarkers;
            Expression[] spilledArgs;
            Expression[] callArgs = GetArgumentExpressions(restrictedArgs, out usageMarkers, out spilledArgs);

            Expression call;
            MethodBase mb = _overload.ReflectionInfo;

            // TODO: make MakeExpression virtual on OverloadInfo?
            if (mb == null)
            {
                throw new InvalidOperationException("Cannot generate an expression for an overload w/o MethodBase");
            }

            MethodInfo mi = mb as MethodInfo;

            if (mi != null)
            {
                Expression instance;
                if (mi.IsStatic)
                {
                    instance = null;
                }
                else
                {
                    Debug.Assert(mi != null);
                    instance = _instanceBuilder.ToExpression(ref mi, _resolver, restrictedArgs, usageMarkers);
                    Debug.Assert(instance != null, "Can't skip instance expression");
                }

                if (CompilerHelpers.IsVisible(mi))
                {
                    call = AstUtils.SimpleCallHelper(instance, mi, callArgs);
                }
                else
                {
                    call = Ast.Call(
                        typeof(BinderOps).GetMethod("InvokeMethod"),
                        AstUtils.Constant(mi),
                        instance != null ? AstUtils.Convert(instance, typeof(object)) : AstUtils.Constant(null),
                        AstUtils.NewArrayHelper(typeof(object), callArgs)
                        );
                }
            }
            else
            {
                ConstructorInfo ci = (ConstructorInfo)mb;
                if (CompilerHelpers.IsVisible(ci))
                {
                    call = AstUtils.SimpleNewHelper(ci, callArgs);
                }
                else
                {
                    call = Ast.Call(
                        typeof(BinderOps).GetMethod("InvokeConstructor"),
                        AstUtils.Constant(ci),
                        AstUtils.NewArrayHelper(typeof(object), callArgs)
                        );
                }
            }

            if (spilledArgs != null)
            {
                call = Expression.Block(spilledArgs.AddLast(call));
            }

            Expression ret = _returnBuilder.ToExpression(_resolver, _argBuilders, restrictedArgs, call);

            List <Expression> updates = null;

            for (int i = 0; i < _argBuilders.Count; i++)
            {
                Expression next = _argBuilders[i].UpdateFromReturn(_resolver, restrictedArgs);
                if (next != null)
                {
                    if (updates == null)
                    {
                        updates = new List <Expression>();
                    }
                    updates.Add(next);
                }
            }

            if (updates != null)
            {
                if (ret.Type != typeof(void))
                {
                    ParameterExpression temp = Ast.Variable(ret.Type, "$ret");
                    updates.Insert(0, Ast.Assign(temp, ret));
                    updates.Add(temp);
                    ret = Ast.Block(new[] { temp }, updates.ToArray());
                }
                else
                {
                    updates.Insert(0, ret);
                    ret = Ast.Block(typeof(void), updates.ToArray());
                }
            }

            if (_resolver.Temps != null)
            {
                ret = Ast.Block(_resolver.Temps, ret);
            }

            return(ret);
        }