private Expression RebindEntityMemberInit(MemberInitExpression init) { Debug.Assert(init != null, "init != null"); Debug.Assert(init.Bindings.Count > 0, "init.Bindings.Count > 0 -- otherwise this is just empty construction"); Expression[] expressions; if (!this.pathBuilder.HasRewrites) { MemberAssignmentAnalysis propertyAnalysis = MemberAssignmentAnalysis.Analyze( this.pathBuilder.LambdaParameterInScope, ((MemberAssignment)init.Bindings[0]).Expression); expressions = propertyAnalysis.GetExpressionsToTargetEntity(); Debug.Assert(expressions.Length != 0, "expressions.Length != 0 -- otherwise there is no correlation to parameter in entity member init"); } else { expressions = MemberAssignmentAnalysis.EmptyExpressionArray; } Expression entryParameterAtMemberInit = this.pathBuilder.ParameterEntryInScope; List <string> propertyNames = new List <string>(); List <Func <object, object, Type, object> > propertyFunctions = new List <Func <object, object, Type, object> >(); Type projectedType = init.NewExpression.Type; Expression projectedTypeExpression = Expression.Constant(projectedType, typeof(Type)); Expression entryToInitValue; Expression expectedParamValue; ParameterExpression entryParameterForMembers; ParameterExpression expectedParameterForMembers; string[] expressionNames = expressions.Skip(1).Select(e => ((MemberExpression)e).Member.Name).ToArray(); if (expressions.Length <= 1) { entryToInitValue = this.pathBuilder.ParameterEntryInScope; expectedParamValue = this.pathBuilder.ExpectedParamTypeInScope; entryParameterForMembers = (ParameterExpression)this.pathBuilder.ParameterEntryInScope; expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope; } else { entryToInitValue = this.GetDeepestEntry(expressions); expectedParamValue = projectedTypeExpression; entryParameterForMembers = Expression.Parameter(typeof(object), "subentry" + this.identifierId++); expectedParameterForMembers = (ParameterExpression)this.pathBuilder.ExpectedParamTypeInScope; ProjectionPath entryPath = new ProjectionPath( (ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, this.pathBuilder.ParameterEntryInScope, expressions.Skip(1)); this.annotations.Add(entryToInitValue, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] }); this.annotations.Add(entryParameterForMembers, new ExpressionAnnotation() { Segment = entryPath[entryPath.Count - 1] }); this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames, entryParameterForMembers); } for (int i = 0; i < init.Bindings.Count; i++) { MemberAssignment assignment = (MemberAssignment)init.Bindings[i]; propertyNames.Add(assignment.Member.Name); LambdaExpression propertyLambda; if ((ClientType.CheckElementTypeIsEntity(assignment.Member.ReflectedType) && assignment.Expression.NodeType == ExpressionType.MemberInit)) { Expression nestedEntry = CallMaterializer( "ProjectionGetEntry", entryParameterAtMemberInit, Expression.Constant(assignment.Member.Name, typeof(string))); ParameterExpression nestedEntryParameter = Expression.Parameter( typeof(object), "subentry" + this.identifierId++); ProjectionPath entryPath; ExpressionAnnotation entryAnnotation; if (this.annotations.TryGetValue(this.pathBuilder.ParameterEntryInScope, out entryAnnotation)) { entryPath = new ProjectionPath( (ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, entryParameterAtMemberInit); entryPath.AddRange(entryAnnotation.Segment.StartPath); } else { entryPath = new ProjectionPath( (ParameterExpression)this.pathBuilder.LambdaParameterInScope, this.pathBuilder.ExpectedParamTypeInScope, entryParameterAtMemberInit, expressions.Skip(1)); } ProjectionPathSegment nestedSegment = new ProjectionPathSegment( entryPath, assignment.Member.Name, assignment.Member.ReflectedType); entryPath.Add(nestedSegment); string[] names = (entryPath.Where(m => m.Member != null).Select(m => m.Member)).ToArray(); this.annotations.Add(nestedEntryParameter, new ExpressionAnnotation() { Segment = nestedSegment }); this.pathBuilder.RegisterRewrite(this.pathBuilder.LambdaParameterInScope, names, nestedEntryParameter); Expression e = this.Visit(assignment.Expression); this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, names); this.annotations.Remove(nestedEntryParameter); e = Expression.Convert(e, typeof(object)); ParameterExpression[] parameters = new ParameterExpression[] { this.materializerExpression, nestedEntryParameter, expectedParameterForMembers, }; propertyLambda = Expression.Lambda(e, parameters); Expression[] nestedParams = new Expression[] { this.materializerExpression, nestedEntry, expectedParameterForMembers, }; var invokeParameters = new ParameterExpression[] { this.materializerExpression, (ParameterExpression)entryParameterAtMemberInit, expectedParameterForMembers, }; propertyLambda = Expression.Lambda(Expression.Invoke(propertyLambda, nestedParams), invokeParameters); } else { Expression e = this.Visit(assignment.Expression); e = Expression.Convert(e, typeof(object)); ParameterExpression[] parameters = new ParameterExpression[] { this.materializerExpression, entryParameterForMembers, expectedParameterForMembers, }; propertyLambda = Expression.Lambda(e, parameters); } #if TRACE_CLIENT_PROJECTIONS Trace.WriteLine("Compiling lambda for " + assignment.Member.Name + ": " + propertyLambda); #endif propertyFunctions.Add((Func <object, object, Type, object>)propertyLambda.Compile()); } for (int i = 1; i < expressions.Length; i++) { this.pathBuilder.RevokeRewrite(this.pathBuilder.LambdaParameterInScope, expressionNames); this.annotations.Remove(entryToInitValue); this.annotations.Remove(entryParameterForMembers); } Expression reboundExpression = CallMaterializer( "ProjectionInitializeEntity", this.materializerExpression, entryToInitValue, expectedParamValue, projectedTypeExpression, Expression.Constant(propertyNames.ToArray()), Expression.Constant(propertyFunctions.ToArray())); return(Expression.Convert(reboundExpression, projectedType)); }
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)); }