Пример #1
0
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Debug.Assert(m != null, "m != null");
            Debug.Assert(baseAnnotation != null, "baseAnnotation != null");

            ProjectionPathSegment memberSegment;

            Expression baseSourceExpression = m.Expression;
            Expression result = this.pathBuilder.GetRewrite(baseSourceExpression);

            if (result != null)
            {
                Expression            baseTypeExpression = Expression.Constant(baseSourceExpression.Type, typeof(Type));
                ProjectionPath        nestedPath         = new ProjectionPath(result as ParameterExpression, baseTypeExpression, result);
                ProjectionPathSegment nestedSegment      = new ProjectionPathSegment(nestedPath, m.Member.Name, m.Type);
                nestedPath.Add(nestedSegment);
                result = this.CallValueForPathWithType(result, baseTypeExpression, nestedPath, m.Type);
            }
            else
            {
                memberSegment = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m.Member.Name, m.Type);
                baseAnnotation.Segment.StartPath.Add(memberSegment);
                result = this.CallValueForPathWithType(
                    baseAnnotation.Segment.StartPath.RootEntry,
                    baseAnnotation.Segment.StartPath.ExpectedRootType,
                    baseAnnotation.Segment.StartPath,
                    m.Type);
            }

            return(result);
        }
Пример #2
0
 internal override Expression VisitLambda(LambdaExpression lambda)
 {
     if (!this.topLevelProjectionFound || ((lambda.Parameters.Count == 1) && ClientTypeUtil.TypeOrElementTypeIsEntity(lambda.Parameters[0].Type)))
     {
         this.topLevelProjectionFound = true;
         ParameterExpression expectedType = Expression.Parameter(typeof(Type), "type" + this.identifierId);
         ParameterExpression entry        = Expression.Parameter(typeof(object), "entry" + this.identifierId);
         this.identifierId++;
         this.pathBuilder.EnterLambdaScope(lambda, entry, expectedType);
         ProjectionPath        startPath = new ProjectionPath(lambda.Parameters[0], expectedType, entry);
         ProjectionPathSegment item      = new ProjectionPathSegment(startPath, null, null);
         startPath.Add(item);
         ExpressionAnnotation annotation = new ExpressionAnnotation {
             Segment = item
         };
         this.annotations[lambda.Parameters[0]] = annotation;
         Expression expression4 = this.Visit(lambda.Body);
         if (expression4.Type.IsValueType)
         {
             expression4 = Expression.Convert(expression4, typeof(object));
         }
         Expression expression = Expression.Lambda <Func <object, object, Type, object> >(expression4, new ParameterExpression[] { this.materializerExpression, entry, expectedType });
         this.pathBuilder.LeaveLambdaScope();
         return(expression);
     }
     return(base.VisitLambda(lambda));
 }
Пример #3
0
        private Expression RebindNewExpressionForDataServiceCollectionOfT(NewExpression nex)
        {
            NewExpression        key         = this.VisitNew(nex);
            Expression           expression2 = null;
            ExpressionAnnotation annotation  = null;

            if (key != null)
            {
                ConstructorInfo info = nex.Type.GetInstanceConstructors(false).First <ConstructorInfo>(c => (c.GetParameters().Length == 7) && (c.GetParameters()[0].ParameterType == typeof(object)));
                Type            type = typeof(IEnumerable <>).MakeGenericType(new Type[] { nex.Type.GetGenericArguments()[0] });
                if (((key.Arguments.Count == 1) && (key.Constructor == nex.Type.GetInstanceConstructor(true, new Type[] { type }))) && this.annotations.TryGetValue(key.Arguments[0], out annotation))
                {
                    expression2 = RebindConstructor(info, new Expression[] { this.materializerExpression, Expression.Constant(null, typeof(DataServiceContext)), key.Arguments[0], Expression.Constant(TrackingMode.AutoChangeTracking, typeof(TrackingMode)), Expression.Constant(null, typeof(string)), Expression.Constant(null, typeof(Func <EntityChangedParams, bool>)), Expression.Constant(null, typeof(Func <EntityCollectionChangedParams, bool>)) });
                }
                else if ((key.Arguments.Count == 2) && this.annotations.TryGetValue(key.Arguments[0], out annotation))
                {
                    expression2 = RebindConstructor(info, new Expression[] { this.materializerExpression, Expression.Constant(null, typeof(DataServiceContext)), key.Arguments[0], key.Arguments[1], Expression.Constant(null, typeof(string)), Expression.Constant(null, typeof(Func <EntityChangedParams, bool>)), Expression.Constant(null, typeof(Func <EntityCollectionChangedParams, bool>)) });
                }
                else if ((key.Arguments.Count == 5) && this.annotations.TryGetValue(key.Arguments[0], out annotation))
                {
                    expression2 = RebindConstructor(info, new Expression[] { this.materializerExpression, Expression.Constant(null, typeof(DataServiceContext)), key.Arguments[0], key.Arguments[1], key.Arguments[2], key.Arguments[3], key.Arguments[4] });
                }
                else if (((key.Arguments.Count == 6) && typeof(DataServiceContext).IsAssignableFrom(key.Arguments[0].Type)) && this.annotations.TryGetValue(key.Arguments[1], out annotation))
                {
                    expression2 = RebindConstructor(info, new Expression[] { this.materializerExpression, key.Arguments[0], key.Arguments[1], key.Arguments[2], key.Arguments[3], key.Arguments[4], key.Arguments[5] });
                }
            }
            if (annotation != null)
            {
                this.annotations.Add(key, annotation);
            }
            return(expression2);
        }
Пример #4
0
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation)
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(annotation != null, "annotation != null");

            Expression result;

            result = this.CallValueForPathWithType(
                annotation.Segment.StartPath.RootEntry,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath,
                expression.Type);

            ProjectionPath parameterPath = new ProjectionPath(
                annotation.Segment.StartPath.Root,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null);

            parameterPath.Add(parameterSegment);
            this.annotations[expression] = new ExpressionAnnotation()
            {
                Segment = parameterSegment
            };

            return(result);
        }
Пример #5
0
 private Expression CallValueForPathWithType(Expression entry, Expression entryType, ProjectionPath path, Type type)
 {
     Expression key = Expression.Convert(this.CallValueForPath(entry, entryType, path), type);
     ExpressionAnnotation annotation = new ExpressionAnnotation {
         Segment = path[path.Count - 1]
     };
     this.annotations.Add(key, annotation);
     return key;
 }
Пример #6
0
 private Expression CallValueForPath(Expression entry, Expression entryType, ProjectionPath path)
 {
     Expression key = CallMaterializer("ProjectionValueForPath", new Expression[] { this.materializerExpression, entry, entryType, Expression.Constant(path, typeof(object)) });
     ExpressionAnnotation annotation = new ExpressionAnnotation {
         Segment = path[path.Count - 1]
     };
     this.annotations.Add(key, annotation);
     return key;
 }
Пример #7
0
        private Expression CallValueForPathWithType(Expression entry, Expression entryType, ProjectionPath path, Type type)
        {
            Expression           key        = Expression.Convert(this.CallValueForPath(entry, entryType, path), type);
            ExpressionAnnotation annotation = new ExpressionAnnotation {
                Segment = path[path.Count - 1]
            };

            this.annotations.Add(key, annotation);
            return(key);
        }
Пример #8
0
        private Expression CallValueForPath(Expression entry, Expression entryType, ProjectionPath path)
        {
            Expression           key        = CallMaterializer("ProjectionValueForPath", new Expression[] { this.materializerExpression, entry, entryType, Expression.Constant(path, typeof(object)) });
            ExpressionAnnotation annotation = new ExpressionAnnotation {
                Segment = path[path.Count - 1]
            };

            this.annotations.Add(key, annotation);
            return(key);
        }
Пример #9
0
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation)
        {
            Expression            expression2 = this.CallValueForPathWithType(annotation.Segment.StartPath.RootEntry, annotation.Segment.StartPath.ExpectedRootType, annotation.Segment.StartPath, expression.Type);
            ProjectionPath        startPath   = new ProjectionPath(annotation.Segment.StartPath.Root, annotation.Segment.StartPath.ExpectedRootType, annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment item        = new ProjectionPathSegment(startPath, null, null);

            startPath.Add(item);
            ExpressionAnnotation annotation2 = new ExpressionAnnotation {
                Segment = item
            };

            this.annotations[expression] = annotation2;
            return(expression2);
        }
Пример #10
0
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Expression expression = m.Expression;
            Expression rewrite    = this.pathBuilder.GetRewrite(expression);

            if (rewrite != null)
            {
                Expression            expectedRootType = Expression.Constant(expression.Type, typeof(Type));
                ProjectionPath        startPath        = new ProjectionPath(rewrite as ParameterExpression, expectedRootType, rewrite);
                ProjectionPathSegment segment2         = new ProjectionPathSegment(startPath, m);
                startPath.Add(segment2);
                return(this.CallValueForPathWithType(rewrite, expectedRootType, startPath, m.Type));
            }
            ProjectionPathSegment item = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m);

            baseAnnotation.Segment.StartPath.Add(item);
            return(this.CallValueForPathWithType(baseAnnotation.Segment.StartPath.RootEntry, baseAnnotation.Segment.StartPath.ExpectedRootType, baseAnnotation.Segment.StartPath, m.Type));
        }
Пример #11
0
        /// <summary>Rebinds the specified parameter expression as a path-based access.</summary>
        /// <param name="expression">Expression to rebind.</param>
        /// <param name='annotation'>Annotation for the expression to rebind.</param>
        /// <returns>The rebound expression.</returns>
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation)
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(annotation != null, "annotation != null");

            Expression result;
            result = this.CallValueForPathWithType(
                annotation.Segment.StartPath.RootEntry,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath,
                expression.Type);

            // Refresh the annotation so the next one that comes along
            // doesn't start off with an already-written path.
            ProjectionPath parameterPath = new ProjectionPath(
                annotation.Segment.StartPath.Root,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null);
            parameterPath.Add(parameterSegment);
            this.annotations[expression] = new ExpressionAnnotation() { Segment = parameterSegment };

            return result;
        }
Пример #12
0
 internal override Expression VisitLambda(LambdaExpression lambda)
 {
     if (!this.topLevelProjectionFound || ((lambda.Parameters.Count == 1) && ClientTypeUtil.TypeOrElementTypeIsEntity(lambda.Parameters[0].Type)))
     {
         this.topLevelProjectionFound = true;
         ParameterExpression expectedType = Expression.Parameter(typeof(Type), "type" + this.identifierId);
         ParameterExpression entry = Expression.Parameter(typeof(object), "entry" + this.identifierId);
         this.identifierId++;
         this.pathBuilder.EnterLambdaScope(lambda, entry, expectedType);
         ProjectionPath startPath = new ProjectionPath(lambda.Parameters[0], expectedType, entry);
         ProjectionPathSegment item = new ProjectionPathSegment(startPath, null, null);
         startPath.Add(item);
         ExpressionAnnotation annotation = new ExpressionAnnotation {
             Segment = item
         };
         this.annotations[lambda.Parameters[0]] = annotation;
         Expression expression4 = this.Visit(lambda.Body);
         if (expression4.Type.IsValueType)
         {
             expression4 = Expression.Convert(expression4, typeof(object));
         }
         Expression expression = Expression.Lambda<Func<object, object, Type, object>>(expression4, new ParameterExpression[] { this.materializerExpression, entry, expectedType });
         this.pathBuilder.LeaveLambdaScope();
         return expression;
     }
     return base.VisitLambda(lambda);
 }
Пример #13
0
 private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation)
 {
     Expression expression2 = this.CallValueForPathWithType(annotation.Segment.StartPath.RootEntry, annotation.Segment.StartPath.ExpectedRootType, annotation.Segment.StartPath, expression.Type);
     ProjectionPath startPath = new ProjectionPath(annotation.Segment.StartPath.Root, annotation.Segment.StartPath.ExpectedRootType, annotation.Segment.StartPath.RootEntry);
     ProjectionPathSegment item = new ProjectionPathSegment(startPath, null, null);
     startPath.Add(item);
     ExpressionAnnotation annotation2 = new ExpressionAnnotation {
         Segment = item
     };
     this.annotations[expression] = annotation2;
     return expression2;
 }
Пример #14
0
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Debug.Assert(m != null, "m != null");
            Debug.Assert(baseAnnotation != null, "baseAnnotation != null");

            ProjectionPathSegment memberSegment;

            Expression baseSourceExpression = m.Expression;
            Expression result = this.pathBuilder.GetRewrite(baseSourceExpression);
            if (result != null)
            {
                Expression baseTypeExpression = Expression.Constant(baseSourceExpression.Type, typeof(Type));
                ProjectionPath nestedPath = new ProjectionPath(result as ParameterExpression, baseTypeExpression, result);
                ProjectionPathSegment nestedSegment = new ProjectionPathSegment(nestedPath, m.Member.Name, m.Type);
                nestedPath.Add(nestedSegment);
                result = this.CallValueForPathWithType(result, baseTypeExpression, nestedPath, m.Type);
            }
            else
            {
                memberSegment = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m.Member.Name, m.Type);
                baseAnnotation.Segment.StartPath.Add(memberSegment);
                result = this.CallValueForPathWithType(
                    baseAnnotation.Segment.StartPath.RootEntry,
                    baseAnnotation.Segment.StartPath.ExpectedRootType,
                    baseAnnotation.Segment.StartPath,
                    m.Type);
            }

            return result;
        }
Пример #15
0
        private Expression RebindParameter(Expression expression, ExpressionAnnotation annotation)
        {
            Debug.Assert(expression != null, "expression != null");
            Debug.Assert(annotation != null, "annotation != null");

            Expression result;
            result = this.CallValueForPathWithType(
                annotation.Segment.StartPath.RootEntry,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath,
                expression.Type);

            ProjectionPath parameterPath = new ProjectionPath(
                annotation.Segment.StartPath.Root,
                annotation.Segment.StartPath.ExpectedRootType,
                annotation.Segment.StartPath.RootEntry);
            ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null);
            parameterPath.Add(parameterSegment);
            this.annotations[expression] = new ExpressionAnnotation() { Segment = parameterSegment };

            return result;
        }
Пример #16
0
        /// <summary>Rebinds the specified member access expression into a path-based value retrieval method call.</summary>
        /// <param name='m'>Member expression.</param>
        /// <param name='baseAnnotation'>Annotation for the base portion of the expression.</param>
        /// <returns>A rebound expression.</returns>
        private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
        {
            Debug.Assert(m != null, "m != null");
            Debug.Assert(baseAnnotation != null, "baseAnnotation != null");

            ProjectionPathSegment memberSegment;

            // If we are in nested member-init, we rewrite the property
            // accessors that are in the form of top.nested.id to
            // nested.id.
            Expression baseSourceExpression = m.Expression;
            Expression result = this.pathBuilder.GetRewrite(baseSourceExpression);
            if (result != null)
            {
                Expression baseTypeExpression = Expression.Constant(baseSourceExpression.Type, typeof(Type));
                ProjectionPath nestedPath = new ProjectionPath(result as ParameterExpression, baseTypeExpression, result);
                ProjectionPathSegment nestedSegment = new ProjectionPathSegment(nestedPath, m.Member.Name, m.Type);
                nestedPath.Add(nestedSegment);
                result = this.CallValueForPathWithType(result, baseTypeExpression, nestedPath, m.Type);
            }
            else
            {
                // This actually modifies the path for the underlying
                // segments, but that shouldn't be a problem. Actually
                // we should be able to remove it from the dictionary.
                // There should be no aliasing problems, because
                // annotations always come from target expression
                // that are generated anew (except parameters,
                // but those)
                memberSegment = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m.Member.Name, m.Type);
                baseAnnotation.Segment.StartPath.Add(memberSegment);
                result = this.CallValueForPathWithType(
                    baseAnnotation.Segment.StartPath.RootEntry,
                    baseAnnotation.Segment.StartPath.ExpectedRootType,
                    baseAnnotation.Segment.StartPath,
                    m.Type);
            }

            return result;
        }
Пример #17
0
 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);
 }
Пример #18
0
 private Expression RebindMemberAccess(MemberExpression m, ExpressionAnnotation baseAnnotation)
 {
     Expression expression = m.Expression;
     Expression rewrite = this.pathBuilder.GetRewrite(expression);
     if (rewrite != null)
     {
         Expression expectedRootType = Expression.Constant(expression.Type, typeof(Type));
         ProjectionPath startPath = new ProjectionPath(rewrite as ParameterExpression, expectedRootType, rewrite);
         ProjectionPathSegment segment2 = new ProjectionPathSegment(startPath, m);
         startPath.Add(segment2);
         return this.CallValueForPathWithType(rewrite, expectedRootType, startPath, m.Type);
     }
     ProjectionPathSegment item = new ProjectionPathSegment(baseAnnotation.Segment.StartPath, m);
     baseAnnotation.Segment.StartPath.Add(item);
     return this.CallValueForPathWithType(baseAnnotation.Segment.StartPath.RootEntry, baseAnnotation.Segment.StartPath.ExpectedRootType, baseAnnotation.Segment.StartPath, m.Type);
 }
Пример #19
0
        private NewExpression RebindNewExpressionForDataServiceCollectionOfT(NewExpression nex)
        {
            Debug.Assert(nex != null, "nex != null");
            Debug.Assert(
                ResourceBinder.PatternRules.MatchNewDataServiceCollectionOfT(nex),
                "Called should have checked that the 'new' was for our collection type");

            NewExpression result = base.VisitNew(nex);

            ExpressionAnnotation annotation = null;

            if (result != null)
            {
                ConstructorInfo constructorInfo =
                    nex.Type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(
                        c => c.GetParameters().Length == 7 && c.GetParameters()[0].ParameterType == typeof(object));

                Type enumerable = typeof(IEnumerable <>).MakeGenericType(nex.Type.GetGenericArguments()[0]);

                if (result.Arguments.Count == 1 && result.Constructor == nex.Type.GetConstructor(new[] { enumerable }) &&
                    this.annotations.TryGetValue(result.Arguments[0], out annotation))
                {
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)),
                        result.Arguments[0],
                        Expression.Constant(TrackingMode.AutoChangeTracking, typeof(TrackingMode)),
                        Expression.Constant(null, typeof(string)),
                        Expression.Constant(null, typeof(Func <EntityChangedParams, bool>)),
                        Expression.Constant(null, typeof(Func <EntityCollectionChangedParams, bool>)));
                }
                else if (result.Arguments.Count == 2 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                {
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)),
                        result.Arguments[0], result.Arguments[1], Expression.Constant(null, typeof(string)),
                        Expression.Constant(null, typeof(Func <EntityChangedParams, bool>)),
                        Expression.Constant(null, typeof(Func <EntityCollectionChangedParams, bool>)));
                }
                else if (result.Arguments.Count == 5 &&
                         this.annotations.TryGetValue(result.Arguments[0], out annotation))
                {
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        Expression.Constant(null, typeof(DataServiceContext)),
                        result.Arguments[0], result.Arguments[1], result.Arguments[2], result.Arguments[3], result.Arguments[4]);
                }
                else if (result.Arguments.Count == 6 &&
                         typeof(DataServiceContext).IsAssignableFrom(result.Arguments[0].Type) &&
                         this.annotations.TryGetValue(result.Arguments[1], out annotation))
                {
                    result = Expression.New(
                        constructorInfo,
                        this.materializerExpression,
                        result.Arguments[0], result.Arguments[1], result.Arguments[2], result.Arguments[3], result.Arguments[4], result.Arguments[5]);
                }
            }

            if (annotation != null)
            {
                this.annotations.Add(result, annotation);
            }

            return(result);
        }
Пример #20
0
        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));
        }