Exemplo n.º 1
0
            private CodeSnippetExpression RewriteCreateAndInitializeObjectExpression(CodeCreateAndInitializeObjectExpression objectCreateExpression)
            {
                string separator = string.Empty;

                using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                {
                    sw.Write("new");
                    if (objectCreateExpression.ObjectType != null)
                    {
                        sw.Write(" ");
                        sw.Write(this.generator.GetTypeOutput(objectCreateExpression.ObjectType));
                        sw.Write("(");

                        separator = string.Empty;
                        foreach (CodeExpression expr in objectCreateExpression.ConstructorParameters)
                        {
                            sw.Write(separator);
                            separator = ", ";

                            this.generator.GenerateCodeFromExpression(expr, sw, null);
                        }

                        sw.Write(")");
                    }

                    sw.WriteLine();
                    this.WriteIndentation(sw);

                    // We shouldn't write out the '{ }' marker if no property initializers are passed in to the object creation expression
                    if (objectCreateExpression.PropertyInitializers.Any())
                    {
                        sw.WriteLine("{");
                        this.nestingLevel++;
                        try
                        {
                            foreach (var kvp in objectCreateExpression.PropertyInitializers)
                            {
                                this.WriteIndentation(sw);
                                if (kvp.Key != null)
                                {
                                    sw.Write(kvp.Key);
                                    sw.Write(" = ");
                                }

                                this.generator.GenerateCodeFromExpression(kvp.Value, sw, null);
                                sw.WriteLine(", ");
                            }
                        }
                        finally
                        {
                            this.nestingLevel--;
                        }

                        this.WriteIndentation(sw);
                        sw.Write("}");
                    }

                    return(new CodeSnippetExpression(sw.ToString()));
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Generates System.CodeDom.CodeExpression from the given expression.
        /// </summary>
        /// <param name="expression">Expression from which System.CodeDom.CodeExpression is generated.</param>
        /// <returns>Generated System.CodeDom.CodeExpression.</returns>
        public virtual CodeExpression Visit(LinqNewExpression expression)
        {
            CodeCreateAndInitializeObjectExpression result = null;

            result = new CodeCreateAndInitializeObjectExpression();

            for (int i = 0; i < expression.Members.Count; i++)
            {
                CodeExpression member = this.GenerateCode(expression.Members[i]);
                member = this.FixupPropertyForPropertyInitializer(expression, expression.MemberNames[i], expression.Members[i], member);
                result.PropertyInitializers.Add(new KeyValuePair <string, CodeExpression>(expression.MemberNames[i], member));
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates System.CodeDom.CodeExpression from the given expression.
        /// </summary>
        /// <param name="expression">Expression from which System.CodeDom.CodeExpression is generated.</param>
        /// <returns>Generated System.CodeDom.CodeExpression.</returns>
        public virtual CodeExpression Visit(LinqNewInstanceExpression expression)
        {
            CodeCreateAndInitializeObjectExpression result = null;
            var constructorArguments = expression.ConstructorArguments.Select(this.GenerateCode);

            result = new CodeCreateAndInitializeObjectExpression(constructorArguments.ToArray());
            var queryClrType = expression.ExpressionType as IQueryClrType;

            ExceptionUtilities.CheckObjectNotNull(queryClrType, "Type must implement " + typeof(IQueryClrType).Name + "interface: " + expression.ExpressionType + ".");

            CodeTypeReference entityTypeReference = Code.TypeRef(queryClrType.ClrType);

            result.ObjectType = entityTypeReference;

            for (int i = 0; i < expression.Members.Count; i++)
            {
                CodeExpression member = this.GenerateCode(expression.Members[i]);
                member = this.FixupPropertyForPropertyInitializer(expression, expression.MemberNames[i], expression.Members[i], member);
                result.PropertyInitializers.Add(new KeyValuePair <string, CodeExpression>(expression.MemberNames[i], member));
            }

            return(result);
        }
Exemplo n.º 4
0
            /// <summary>
            /// Rewrites CodeExpression
            /// </summary>
            /// <param name="e">A CodeExpression to be rewritten.</param>
            /// <param name="didRewrite">A value which will be set to true if the rewriting returned a new object.</param>
            /// <returns>Rewritten CodeExpression.</returns>
            protected override CodeExpression Rewrite(CodeExpression e, ref bool didRewrite)
            {
                CodeLambdaExpression lambdaExpression = e as CodeLambdaExpression;

                if (lambdaExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteLambdaExpression(lambdaExpression));
                }

                CodeQueryExpression queryExpression = e as CodeQueryExpression;

                if (queryExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteComprehensionExpression(queryExpression));
                }

                CodeCreateAndInitializeObjectExpression objectCreateExpression = e as CodeCreateAndInitializeObjectExpression;

                if (objectCreateExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteCreateAndInitializeObjectExpression(objectCreateExpression));
                }

                CodeArrayInitializerExpression arrayInitializer = e as CodeArrayInitializerExpression;

                if (arrayInitializer != null)
                {
                    didRewrite = true;
                    return(this.RewriteArrayInitializerExpression(arrayInitializer));
                }

                CodeExclusiveOrExpression exclusiveOrExpression = e as CodeExclusiveOrExpression;

                if (exclusiveOrExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteExclusiveOrExpression(exclusiveOrExpression));
                }

                CodeAsExpression asExpression = e as CodeAsExpression;

                if (asExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteAsExpression(asExpression));
                }

                CodeIsExpression isExpression = e as CodeIsExpression;

                if (isExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteIsExpression(isExpression));
                }

                CodePrimitiveExpression primitiveExpression = e as CodePrimitiveExpression;

                if (primitiveExpression != null)
                {
                    return(this.RewritePrimitiveExpression(primitiveExpression, ref didRewrite));
                }

                var ternaryExpression = e as CodeTernaryExpression;

                if (ternaryExpression != null)
                {
                    didRewrite = true;
                    return(this.RewriteTernaryExpression(ternaryExpression));
                }

                var anonymousArray = e as CodeAnonymousArrayExpression;

                if (anonymousArray != null)
                {
                    didRewrite = true;
                    return(this.RewriteAnonymousArrayExpression(anonymousArray));
                }

                return(base.Rewrite(e, ref didRewrite));
            }