Beispiel #1
0
        // MethodCallExpression
        // TODO: ref parameters!!!
        private Expression Rewrite(MethodCallExpression node)
        {
            Expression instance = RewriteExpression(node.Instance);
            ReadOnlyCollection <Expression> args = node.Arguments;

            Expression[] clone = null;
            Expression[] comma = null;
            int          ci    = 0; // comma array fill index

            if (args != null)
            {
                for (int i = 0; i < args.Count; i++)
                {
                    Expression arg  = args[i];
                    Expression rarg = RewriteExpression(arg);

                    if ((object)arg != (object)rarg)
                    {
                        if (clone == null)
                        {
                            clone = new Expression[args.Count];
                            if (instance != null)
                            {
                                comma    = new Expression[args.Count + 2]; // + instance + the call
                                instance = ToTemp(instance, out comma[ci++]);
                            }
                            else
                            {
                                comma = new Expression[args.Count + 1];
                            }

                            for (int j = 0; j < i; j++)
                            {
                                clone[j] = ToTemp(args[j], out comma[ci++]);
                            }
                        }
                    }

                    if (clone != null)
                    {
                        clone[i] = ToTemp(rarg, out comma[ci++]);
                    }
                }
            }

            if (clone != null)
            {
                comma[ci] = Ast.Call(instance, node.Method, clone);
                return(Ast.Comma(comma));
            }
            else if ((object)instance != (object)node.Instance)
            {
                return(Ast.Call(
                           instance,
                           node.Method,
                           node.Arguments
                           ));
            }
            else
            {
                return(node);
            }
        }