コード例 #1
0
        /// <summary>
        /// Reads a new anonymous type allocation expression from the code.
        /// </summary>
        /// <param name="expressionProxy">The proxy object for this expression.</param>
        /// <param name="parentProxy">The proxy object for the parent.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <returns>Returns the expression.</returns>
        private NewExpression GetNewAnonymousTypeExpression(CodeUnitProxy expressionProxy, CodeUnitProxy parentProxy, bool unsafeCode)
        {
            Param.AssertNotNull(expressionProxy, "expressionProxy");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(unsafeCode);

            this.AdvanceToNextCodeSymbol(expressionProxy);

            // Get the anonymous type initializer expression.
            CollectionInitializerExpression initializer = this.GetAnonymousTypeInitializerExpression(expressionProxy, unsafeCode);

            // Create and return the expression.
            var expression = new NewExpression(expressionProxy, null, initializer);
            parentProxy.Children.Add(expression);

            return expression;
        }
コード例 #2
0
        /// <summary>
        /// Reads a new non-array type allocation expression from the code.
        /// </summary>
        /// <param name="expressionProxy">The proxy object for this expression.</param>
        /// <param name="parentProxy">The proxy object for the parent.</param>
        /// <param name="unsafeCode">Indicates whether the code being parsed resides in an unsafe code block.</param>
        /// <param name="type">The type of the array.</param>
        /// <returns>Returns the expression.</returns>
        private NewExpression GetNewNonArrayTypeExpression(CodeUnitProxy expressionProxy, CodeUnitProxy parentProxy, bool unsafeCode, Expression type)
        {
            Param.AssertNotNull(expressionProxy, "expressionProxy");
            Param.AssertNotNull(parentProxy, "parentProxy");
            Param.Ignore(unsafeCode);
            Param.AssertNotNull(type, "type");

            this.AdvanceToNextCodeSymbol(expressionProxy); 

            Expression typeCreationExpression = type;

            // If the next symbol is an opening parenthesis, then there is an argument
            // list which must be attached to the type creation expression.
            Symbol symbol = this.PeekNextSymbol();

            if (symbol.SymbolType == SymbolType.OpenParenthesis)
            {
                typeCreationExpression = this.GetMethodInvocationExpression(expressionProxy, type, ExpressionPrecedence.None, unsafeCode);
                if (typeCreationExpression == null || typeCreationExpression.Children.Count == 0)
                {
                    throw this.CreateSyntaxException();
                }
            }

            // If the next symbol is an opening curly bracket, then there is an object
            // or collection initializer attached which must also be parsed.
            symbol = this.PeekNextSymbol();

            Expression initializer = null;
            if (symbol.SymbolType == SymbolType.OpenCurlyBracket)
            {
                initializer = this.GetObjectOrCollectionInitializerExpression(expressionProxy, unsafeCode);

                if (initializer == null || initializer.Children.Count == 0)
                {
                    throw this.CreateSyntaxException();
                }
            }

            // Create and return the expression.
            var expression = new NewExpression(expressionProxy, typeCreationExpression, initializer);
            parentProxy.Children.Add(expression);

            return expression;
        }