示例#1
0
            internal static void Analyze(MemberInitExpression mie, PathBox pb, DataServiceContext context)
            {
                ProjectionAnalyzer.EntityProjectionAnalyzer analyzer = new ProjectionAnalyzer.EntityProjectionAnalyzer(pb, mie.Type, context);
                MemberAssignmentAnalysis previous = null;

                foreach (MemberBinding binding in mie.Bindings)
                {
                    MemberAssignment assignment = binding as MemberAssignment;
                    analyzer.Visit(assignment.Expression);
                    if (assignment != null)
                    {
                        MemberAssignmentAnalysis analysis2 = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, assignment.Expression);
                        if (analysis2.IncompatibleAssignmentsException != null)
                        {
                            throw analysis2.IncompatibleAssignmentsException;
                        }
                        Type         memberType = ClientTypeUtil.GetMemberType(assignment.Member);
                        Expression[] expressionsBeyondTargetEntity = analysis2.GetExpressionsBeyondTargetEntity();
                        if (expressionsBeyondTargetEntity.Length == 0)
                        {
                            throw new NotSupportedException(System.Data.Services.Client.Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(memberType, assignment.Expression));
                        }
                        MemberExpression expression = expressionsBeyondTargetEntity[expressionsBeyondTargetEntity.Length - 1] as MemberExpression;
                        analysis2.CheckCompatibleAssignments(mie.Type, ref previous);
                        if (expression != null)
                        {
                            if (expression.Member.Name != assignment.Member.Name)
                            {
                                throw new NotSupportedException(System.Data.Services.Client.Strings.ALinq_PropertyNamesMustMatchInProjections(expression.Member.Name, assignment.Member.Name));
                            }
                            bool flag = ClientTypeUtil.TypeOrElementTypeIsEntity(memberType);
                            if (ClientTypeUtil.TypeOrElementTypeIsEntity(expression.Type) && !flag)
                            {
                                throw new NotSupportedException(System.Data.Services.Client.Strings.ALinq_ExpressionNotSupportedInProjection(memberType, assignment.Expression));
                            }
                        }
                    }
                }
            }
            /// <summary>Analyzes the specified member-init expression.</summary>
            /// <param name="mie">Expression to analyze.</param>
            /// <param name="pb">Path-tracking object to store analysis in.</param>
            /// <param name="context">Context of expression to analyze.</param>
            internal static void Analyze(MemberInitExpression mie, SelectExpandPathBuilder pb, DataServiceContext context)
            {
                Debug.Assert(mie != null, "mie != null");

                var epa = new EntityProjectionAnalyzer(pb, mie.Type, context);

                MemberAssignmentAnalysis targetEntityPath = null;

                foreach (MemberBinding mb in mie.Bindings)
                {
                    MemberAssignment ma = mb as MemberAssignment;
                    epa.Visit(ma.Expression);
                    if (ma != null)
                    {
                        var analysis = MemberAssignmentAnalysis.Analyze(pb.ParamExpressionInScope, ma.Expression);
                        if (analysis.IncompatibleAssignmentsException != null)
                        {
                            throw analysis.IncompatibleAssignmentsException;
                        }

                        // Note that an "empty" assignment on the binding is not checked/handled,
                        // because the funcletizer would have turned that into a constant
                        // in the tree, the visit earlier in this method would have thrown
                        // an exception at finding a constant in an entity projection.
                        //
                        // We do account however for failing to find a reference off the
                        // parameter entry to detect errors like this: new ET() { Ref = e }
                        // Here it looks like the new ET should be the parent of 'e', but
                        // there is nothing in scope that represents that.
                        //
                        // This also explains while error messages might be a bit misleading
                        // in this case (because they reference a constant when the user
                        // hasn't included any).
                        Type         targetType      = ClientTypeUtil.GetMemberType(ma.Member);
                        Expression[] lastExpressions = analysis.GetExpressionsBeyondTargetEntity();
                        if (lastExpressions.Length == 0)
                        {
                            throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjectionToEntity(targetType, ma.Expression));
                        }

                        MemberExpression lastExpression = lastExpressions[lastExpressions.Length - 1] as MemberExpression;
                        Debug.Assert(
                            !analysis.MultiplePathsFound,
                            "!analysis.MultiplePathsFound -- the initilizer has been visited, and cannot be empty, and expressions that can combine paths should have thrown exception during initializer analysis");
#if DEBUG
                        Debug.Assert(
                            lastExpression != null,
                            "lastExpression != null -- the initilizer has been visited, and cannot be empty, and the only expressions that are allowed can be formed off the parameter, so this is always correlatd");
#endif

                        analysis.CheckCompatibleAssignments(mie.Type, ref targetEntityPath);

                        // For DataServiceStreamLink, the last expression will be a constant expression. Hence we won't be comparing name checks and entity checks for those type of bindings
                        if (lastExpression != null)
                        {
                            if (lastExpression.Member.Name != ma.Member.Name)
                            {
                                throw new NotSupportedException(Strings.ALinq_PropertyNamesMustMatchInProjections(lastExpression.Member.Name, ma.Member.Name));
                            }

                            // Unless we're initializing an entity, we should not traverse into the parameter in scope.
                            bool targetIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(targetType);
                            bool sourceIsEntity = ClientTypeUtil.TypeOrElementTypeIsEntity(lastExpression.Type);
                            if (sourceIsEntity && !targetIsEntity)
                            {
                                throw new NotSupportedException(Strings.ALinq_ExpressionNotSupportedInProjection(targetType, ma.Expression));
                            }
                        }
                    }
                }
            }
        private Expression RebindEntityMemberInit(MemberInitExpression init)
        {
            Expression[]        expressionsToTargetEntity;
            Expression          deepestEntry;
            Expression          expectedParamTypeInScope;
            ParameterExpression expression5;
            ParameterExpression expression6;

            if (!this.pathBuilder.HasRewrites)
            {
                expressionsToTargetEntity = MemberAssignmentAnalysis.Analyze(this.pathBuilder.LambdaParameterInScope, ((MemberAssignment)init.Bindings[0]).Expression).GetExpressionsToTargetEntity();
            }
            else
            {
                expressionsToTargetEntity = MemberAssignmentAnalysis.EmptyExpressionArray;
            }
            Expression    parameterEntryInScope = this.pathBuilder.ParameterEntryInScope;
            List <string> list = new List <string>();
            List <Func <object, object, Type, object> > list2 = new List <Func <object, object, Type, object> >();
            Type       type        = init.NewExpression.Type;
            Expression expression2 = Expression.Constant(type, typeof(Type));

            string[] names = (from e in expressionsToTargetEntity.Skip <Expression>(1) select((MemberExpression)e).Member.Name).ToArray <string>();
            if (expressionsToTargetEntity.Length <= 1)
            {
                deepestEntry             = this.pathBuilder.ParameterEntryInScope;
                expectedParamTypeInScope = this.pathBuilder.ExpectedParamTypeInScope;
                expression5 = (ParameterExpression)this.pathBuilder.ParameterEntryInScope;
                expression6 = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope;
            }
            else
            {
                deepestEntry             = this.GetDeepestEntry(expressionsToTargetEntity);
                expectedParamTypeInScope = expression2;
                expression5 = Expression.Parameter(typeof(object), "subentry" + this.identifierId++);
                expression6 = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope;
                ProjectionPath       path       = new ProjectionPath((ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, this.pathBuilder.ParameterEntryInScope, expressionsToTargetEntity.Skip <Expression>(1));
                ExpressionAnnotation annotation = new ExpressionAnnotation {
                    Segment = path[path.Count - 1]
                };
                this.annotations.Add(deepestEntry, annotation);
                ExpressionAnnotation annotation2 = new ExpressionAnnotation {
                    Segment = path[path.Count - 1]
                };
                this.annotations.Add(expression5, annotation2);
                this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, names, expression5);
            }
            for (int i = 0; i < init.Bindings.Count; i++)
            {
                LambdaExpression expression7;
                MemberAssignment assignment = (MemberAssignment)init.Bindings[i];
                list.Add(assignment.Member.Name);
                if (ClientTypeUtil.TypeOrElementTypeIsEntity(ClientTypeUtil.GetMemberType(assignment.Member)) && (assignment.Expression.NodeType == ExpressionType.MemberInit))
                {
                    ProjectionPath       path2;
                    ExpressionAnnotation annotation3;
                    Expression           expression8 = CallMaterializer("ProjectionGetEntry", new Expression[] { parameterEntryInScope, Expression.Constant(assignment.Member.Name, typeof(string)) });
                    ParameterExpression  key         = Expression.Parameter(typeof(object), "subentry" + this.identifierId++);
                    if (this.annotations.TryGetValue(this.pathBuilder.ParameterEntryInScope, out annotation3))
                    {
                        path2 = new ProjectionPath((ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, parameterEntryInScope);
                        path2.AddRange(annotation3.Segment.StartPath);
                    }
                    else
                    {
                        path2 = new ProjectionPath((ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, parameterEntryInScope, expressionsToTargetEntity.Skip <Expression>(1));
                    }
                    Type reflectedType         = assignment.Member.ReflectedType;
                    ProjectionPathSegment item = new ProjectionPathSegment(path2, assignment.Member.Name, reflectedType);
                    path2.Add(item);
                    string[] strArray2 = (from m in path2
                                          where m.Member != null
                                          select m.Member).ToArray <string>();
                    ExpressionAnnotation annotation4 = new ExpressionAnnotation {
                        Segment = item
                    };
                    this.annotations.Add(key, annotation4);
                    this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, strArray2, key);
                    Expression expression = this.Visit(assignment.Expression);
                    this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, strArray2);
                    this.annotations.Remove(key);
                    expression = Expression.Convert(expression, typeof(object));
                    ParameterExpression[] parameters = new ParameterExpression[] { this.materializerExpression, key, expression6 };
                    expression7 = Expression.Lambda(expression, parameters);
                    Expression[]          arguments        = new Expression[] { this.materializerExpression, expression8, expression6 };
                    ParameterExpression[] expressionArray4 = new ParameterExpression[] { this.materializerExpression, (ParameterExpression)parameterEntryInScope, expression6 };
                    expression7 = Expression.Lambda(Expression.Invoke(expression7, arguments), expressionArray4);
                }
                else
                {
                    Expression            body             = Expression.Convert(this.Visit(assignment.Expression), typeof(object));
                    ParameterExpression[] expressionArray5 = new ParameterExpression[] { this.materializerExpression, expression5, expression6 };
                    expression7 = Expression.Lambda(body, expressionArray5);
                }
                list2.Add((Func <object, object, Type, object>)expression7.Compile());
            }
            for (int j = 1; j < expressionsToTargetEntity.Length; j++)
            {
                this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, names);
                this.annotations.Remove(deepestEntry);
                this.annotations.Remove(expression5);
            }
            return(Expression.Convert(CallMaterializer("ProjectionInitializeEntity", new Expression[] { this.materializerExpression, deepestEntry, expectedParamTypeInScope, expression2, Expression.Constant(list.ToArray()), Expression.Constant(list2.ToArray()) }), type));
        }