internal ProjectionPathSegment(ProjectionPath startPath, string member, Type projectionType) { Debug.Assert(startPath != null, "startPath != null"); this.Member = member; this.StartPath = startPath; this.ProjectionType = projectionType; }
/// <summary>Initializes a new <see cref="ProjectionPathSegment"/> instance.</summary> /// <param name="startPath">Path on which this segment is located.</param> /// <param name="member">Name of member to access when traversing a property; possibly null.</param> /// <param name="projectionType"> /// Type that we expect to project out; typically the same as <paramref name="member"/>, but may be adjusted. /// </param> internal ProjectionPathSegment(ProjectionPath startPath, string member, Type projectionType) { Debug.Assert(startPath != null, "startPath != null"); this.Member = member; this.StartPath = startPath; this.ProjectionType = projectionType; }
internal ProjectionPathSegment(ProjectionPath startPath, MemberExpression memberExpression) { this.StartPath = startPath; Expression expression = ResourceBinder.StripTo<Expression>(memberExpression.Expression); this.Member = memberExpression.Member.Name; this.ProjectionType = memberExpression.Type; this.SourceTypeAs = (expression.NodeType == ExpressionType.TypeAs) ? expression.Type : null; }
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; }
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; }
/// <summary>Initializes a new <see cref="ProjectionPathSegment"/> instance.</summary> /// <param name="startPath">Path on which this segment is located.</param> /// <param name="memberExpression">Member expression for the projection path; possibly null.</param> internal ProjectionPathSegment(ProjectionPath startPath, MemberExpression memberExpression) { Debug.Assert(startPath != null, "startPath != null"); Debug.Assert(memberExpression != null, "memberExpression != null"); this.StartPath = startPath; Expression source = ResourceBinder.StripTo<Expression>(memberExpression.Expression); this.Member = ClientTypeUtil.GetServerDefinedName(memberExpression.Member); this.ProjectionType = memberExpression.Type; this.SourceTypeAs = source.NodeType == ExpressionType.TypeAs ? source.Type : null; }
/// <summary>Initializes a new <see cref="ProjectionPathSegment"/> instance.</summary> /// <param name="startPath">Path on which this segment is located.</param> /// <param name="memberExpression">Member expression for the projection path; possibly null.</param> internal ProjectionPathSegment(ProjectionPath startPath, MemberExpression memberExpression) { Debug.Assert(startPath != null, "startPath != null"); Debug.Assert(memberExpression != null, "memberExpression != null"); this.StartPath = startPath; Expression source = ResourceBinder.StripTo <Expression>(memberExpression.Expression); this.Member = ClientTypeUtil.GetServerDefinedName(memberExpression.Member); this.ProjectionType = memberExpression.Type; this.SourceTypeAs = source.NodeType == ExpressionType.TypeAs ? source.Type : null; }
/// <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; }
/// <summary>Creates an expression that calls ProjectionValueForPath.</summary> /// <param name="entry">Expression for root entry for paths.</param> /// <param name="entryType">Expression for expected type for entry.</param> /// <param name="path">Path to pull value from.</param> /// <param name="type">Path to convert result for.</param> /// <returns>A new expression with the call instance.</returns> private Expression CallValueForPathWithType(Expression entry, Expression entryType, ProjectionPath path, Type type) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(path != null, "path != null"); Expression value = this.CallValueForPath(entry, entryType, path); Expression result = Expression.Convert(value, type); this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] }); return result; }
/// <summary>Creates an expression that calls ProjectionCheckValueForPathIsNull.</summary> /// <param name="entry">Expression for root entry for paths.</param> /// <param name="entryType">Expression for expected type for entry.</param> /// <param name="path">Path to check null value for.</param> /// <returns>A new expression with the call instance.</returns> private Expression CallCheckValueForPathIsNull(Expression entry, Expression entryType, ProjectionPath path) { Expression result = CallMaterializer("ProjectionCheckValueForPathIsNull", entry, entryType, Expression.Constant(path, typeof(object))); this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] }); return result; }
internal static object ProjectionValueForPath(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, ProjectionPath path) { if ((path.Count == 0) || ((path.Count == 1) && (path[0].Member == null))) { if (!entry.EntityHasBeenResolved) { materializer.Materialize(entry, expectedType, false); } return(entry.ResolvedObject); } object streamLink = null; ODataNavigationLink link = null; ODataProperty atomProperty = null; ICollection <ODataNavigationLink> navigationLinks = entry.NavigationLinks; IEnumerable <ODataProperty> properties = entry.Entry.Properties; ClientEdmModel model = ClientEdmModel.GetModel(materializer.ResponseInfo.MaxProtocolVersion); for (int i = 0; i < path.Count; i++) { Func <StreamDescriptor, bool> predicate = null; Func <ODataNavigationLink, bool> func2 = null; Func <ODataProperty, bool> func3 = null; Func <ODataProperty, bool> func4 = null; Func <ODataNavigationLink, bool> func5 = null; string propertyName; ProjectionPathSegment segment = path[i]; if (segment.Member != null) { bool flag = i == (path.Count - 1); propertyName = segment.Member; expectedType = segment.SourceTypeAs ?? expectedType; ClientPropertyAnnotation property = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(expectedType)).GetProperty(propertyName, false); if (property.IsStreamLinkProperty) { if (predicate == null) { predicate = sd => sd.Name == propertyName; } StreamDescriptor descriptor = entry.EntityDescriptor.StreamDescriptors.Where <StreamDescriptor>(predicate).SingleOrDefault <StreamDescriptor>(); if (descriptor == null) { if (segment.SourceTypeAs == null) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_PropertyMissing(propertyName, entry.Entry.Id)); } return(WebUtil.GetDefaultValue <DataServiceStreamLink>()); } streamLink = descriptor.StreamLink; } else { if (segment.SourceTypeAs != null) { if (func2 == null) { func2 = p => p.Name == propertyName; } if (!navigationLinks.Any <ODataNavigationLink>(func2)) { if (func3 == null) { func3 = p => p.Name == propertyName; } if (!properties.Any <ODataProperty>(func3) && flag) { return(WebUtil.GetDefaultValue(property.PropertyType)); } } } if (func4 == null) { func4 = p => p.Name == propertyName; } atomProperty = properties.Where <ODataProperty>(func4).FirstOrDefault <ODataProperty>(); if (func5 == null) { func5 = p => p.Name == propertyName; } link = ((atomProperty == null) && (navigationLinks != null)) ? navigationLinks.Where <ODataNavigationLink>(func5).FirstOrDefault <ODataNavigationLink>() : null; if ((link == null) && (atomProperty == null)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_PropertyMissing(propertyName, entry.Entry.Id)); } if (link != null) { ValidatePropertyMatch(property, link); MaterializerNavigationLink link2 = MaterializerNavigationLink.GetLink(link); if (link2.Feed != null) { MaterializerFeed feed = MaterializerFeed.GetFeed(link2.Feed); Type implementationType = ClientTypeUtil.GetImplementationType(segment.ProjectionType, typeof(ICollection <>)); if (implementationType == null) { implementationType = ClientTypeUtil.GetImplementationType(segment.ProjectionType, typeof(IEnumerable <>)); } Type nestedExpectedType = implementationType.GetGenericArguments()[0]; Type projectionType = segment.ProjectionType; if (projectionType.IsInterfaceEx() || ODataMaterializer.IsDataServiceCollection(projectionType)) { projectionType = typeof(Collection <>).MakeGenericType(new Type[] { nestedExpectedType }); } IEnumerable list = (IEnumerable)Util.ActivatorCreateInstance(projectionType, new object[0]); MaterializeToList(materializer, list, nestedExpectedType, feed.Entries); if (ODataMaterializer.IsDataServiceCollection(segment.ProjectionType)) { list = (IEnumerable)Util.ActivatorCreateInstance(WebUtil.GetDataServiceCollectionOfT(new Type[] { nestedExpectedType }), new object[] { list, TrackingMode.None }); } ProjectionPlan plan = CreatePlanForShallowMaterialization(nestedExpectedType); materializer.FoundNextLinkForCollection(list, feed.Feed.NextPageLink, plan); streamLink = list; } else if (link2.Entry != null) { MaterializerEntry entry2 = link2.Entry; if (flag) { if ((entry2.Entry != null) && !entry2.EntityHasBeenResolved) { materializer.Materialize(entry2, property.PropertyType, false); } } else { CheckEntryToAccessNotNull(entry2, propertyName); } properties = entry2.Properties; navigationLinks = entry2.NavigationLinks; streamLink = entry2.ResolvedObject; entry = entry2; } } else { if (atomProperty.Value is ODataStreamReferenceValue) { streamLink = null; navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; continue; } ValidatePropertyMatch(property, atomProperty); if (ClientTypeUtil.TypeOrElementTypeIsEntity(property.PropertyType)) { throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_InvalidEntityType(property.EntityCollectionItemType ?? property.PropertyType)); } if (property.IsPrimitiveOrComplexCollection) { object instance = streamLink ?? (entry.ResolvedObject ?? Util.ActivatorCreateInstance(expectedType, new object[0])); ODataMaterializer.ApplyDataValue(model.GetClientTypeAnnotation(model.GetOrCreateEdmType(instance.GetType())), atomProperty, materializer.ResponseInfo.IgnoreMissingProperties, materializer.ResponseInfo, instance); navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; } else if (atomProperty.Value is ODataComplexValue) { ODataComplexValue complexValue = atomProperty.Value as ODataComplexValue; ODataMaterializer.MaterializeComplexTypeProperty(property.PropertyType, complexValue, materializer.ResponseInfo.IgnoreMissingProperties, materializer.ResponseInfo); properties = complexValue.Properties; navigationLinks = ODataMaterializer.EmptyLinks; } else { if ((atomProperty.Value == null) && !ClientTypeUtil.CanAssignNull(property.NullablePropertyType)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.AtomMaterializer_CannotAssignNull(atomProperty.Name, property.NullablePropertyType)); } ODataMaterializer.MaterializePrimitiveDataValue(property.NullablePropertyType, atomProperty); navigationLinks = ODataMaterializer.EmptyLinks; properties = ODataMaterializer.EmptyProperties; } streamLink = atomProperty.GetMaterializedValue(); } } expectedType = property.PropertyType; } } return(streamLink); }
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; }
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); }
internal ProjectionPathSegment(ProjectionPath startPath, string member, Type projectionType) { this.Member = member; this.StartPath = startPath; this.ProjectionType = projectionType; }
internal static bool ProjectionCheckValueForPathIsNull(MaterializerEntry entry, Type expectedType, ProjectionPath path) { if ((path.Count == 0) || ((path.Count == 1) && (path[0].Member == null))) { return(entry.Entry == null); } bool flag = false; MaterializerNavigationLink link = null; IEnumerable <ODataNavigationLink> navigationLinks = entry.NavigationLinks; ClientEdmModel model = ClientEdmModel.GetModel(entry.EntityDescriptor.MaxProtocolVersion); for (int i = 0; i < path.Count; i++) { Func <ODataNavigationLink, bool> predicate = null; string propertyName; ProjectionPathSegment segment = path[i]; if (segment.Member != null) { bool flag2 = i == (path.Count - 1); propertyName = segment.Member; if (segment.SourceTypeAs != null) { expectedType = segment.SourceTypeAs; if (predicate == null) { predicate = p => p.Name == propertyName; } if (!navigationLinks.Any <ODataNavigationLink>(predicate)) { return(true); } } IEdmType orCreateEdmType = model.GetOrCreateEdmType(expectedType); ClientPropertyAnnotation property = model.GetClientTypeAnnotation(orCreateEdmType).GetProperty(propertyName, false); link = GetPropertyOrThrow(navigationLinks, propertyName, entry.Id); ValidatePropertyMatch(property, link.Link); if (link.Feed != null) { flag = false; } else { if (link.Entry == null) { return(true); } if (flag2) { flag = link.Entry.Entry == null; } else { entry = link.Entry; navigationLinks = entry.NavigationLinks; } } expectedType = property.PropertyType; } } return(flag); }
internal static IEnumerable ProjectionSelect(ODataEntityMaterializer materializer, MaterializerEntry entry, Type expectedType, Type resultType, ProjectionPath path, Func <object, object, Type, object> selector) { ClientEdmModel model = ClientEdmModel.GetModel(materializer.ResponseInfo.MaxProtocolVersion); ClientTypeAnnotation clientTypeAnnotation = entry.ActualType ?? model.GetClientTypeAnnotation(model.GetOrCreateEdmType(expectedType)); IEnumerable enumerable = (IEnumerable)Util.ActivatorCreateInstance(typeof(List <>).MakeGenericType(new Type[] { resultType }), new object[0]); MaterializerNavigationLink link = null; ClientPropertyAnnotation property = null; for (int i = 0; i < path.Count; i++) { ProjectionPathSegment segment = path[i]; if (segment.SourceTypeAs != null) { clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(segment.SourceTypeAs)); } if (segment.Member != null) { string member = segment.Member; property = clientTypeAnnotation.GetProperty(member, false); link = GetPropertyOrThrow(entry.NavigationLinks, member, entry.Id); if (link.Entry != null) { entry = link.Entry; clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(property.PropertyType)); } } } ValidatePropertyMatch(property, link.Link); MaterializerFeed feed = MaterializerFeed.GetFeed(link.Feed); Action <object, object> addToCollectionDelegate = ODataMaterializer.GetAddToCollectionDelegate(enumerable.GetType()); foreach (ODataEntry entry2 in feed.Entries) { object obj2 = selector(materializer, entry2, property.EntityCollectionItemType); addToCollectionDelegate(enumerable, obj2); } ProjectionPlan plan = new ProjectionPlan { LastSegmentType = property.EntityCollectionItemType, Plan = selector, ProjectedType = resultType }; materializer.FoundNextLinkForCollection(enumerable, feed.NextPageLink, plan); return(enumerable); }
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; }
/// <summary>LambdaExpression visit method.</summary> /// <param name="lambda">The LambdaExpression to visit</param> /// <returns>The visited LambdaExpression</returns> internal override Expression VisitLambda(LambdaExpression lambda) { Debug.Assert(lambda != null, "lambda != null"); Expression result; if (!this.topLevelProjectionFound || lambda.Parameters.Count == 1 && ClientType.CheckElementTypeIsEntity(lambda.Parameters[0].Type)) { this.topLevelProjectionFound = true; ParameterExpression expectedTypeParameter = Expression.Parameter(typeof(Type), "type" + this.identifierId); ParameterExpression entryParameter = Expression.Parameter(typeof(object), "entry" + this.identifierId); this.identifierId++; this.pathBuilder.EnterLambdaScope(lambda, entryParameter, expectedTypeParameter); ProjectionPath parameterPath = new ProjectionPath(lambda.Parameters[0], expectedTypeParameter, entryParameter); ProjectionPathSegment parameterSegment = new ProjectionPathSegment(parameterPath, null, null); parameterPath.Add(parameterSegment); this.annotations[lambda.Parameters[0]] = new ExpressionAnnotation() { Segment = parameterSegment }; Expression body = this.Visit(lambda.Body); // Value types must be boxed explicitly; the lambda initialization // won't do it for us (type-compatible types still work, so all // references will work fine with System.Object). if (body.Type.IsValueType) { body = Expression.Convert(body, typeof(object)); } result = Expression.Lambda<Func<object, object, Type, object>>( body, this.materializerExpression, entryParameter, expectedTypeParameter); this.pathBuilder.LeaveLambdaScope(); } else { result = base.VisitLambda(lambda); } return result; }
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); }
/// <summary>Creates an expression that calls ProjectionValueForPath.</summary> /// <param name="entry">Expression for root entry for paths.</param> /// <param name="entryType">Expression for expected type for entry.</param> /// <param name="path">Path to pull value from.</param> /// <returns>A new expression with the call instance.</returns> private Expression CallValueForPath(Expression entry, Expression entryType, ProjectionPath path) { Debug.Assert(entry != null, "entry != null"); Debug.Assert(path != null, "path != null"); Expression result = CallMaterializer("ProjectionValueForPath", this.materializerExpression, entry, entryType, Expression.Constant(path, typeof(object))); this.annotations.Add(result, new ExpressionAnnotation() { Segment = path[path.Count - 1] }); return result; }
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); }
/// <summary> /// Rebinds the specified <paramref name="init"/> expression by gathering /// annotated paths and returning an expression that calls the /// ProjectionGetEntity method. /// </summary> /// <param name="init">Member initialization expression.</param> /// <returns>A new expression suitable for materialization.</returns> 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"); // We "jump" into entities only if we're not already materializing an entity. 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)); // We may need to materialize from deeper in the entity tree for anonymous types. // t => new { nested = new Nested() { nid = t.nested.nid } // // We do the same kind of rewriting we'd do for a nested entity // but at the initializing scope (rather than at the member assignment scope). // // t=> new { nested = ProjInit(GetEntry(entry0, "Nested"), "nid", *->nid) } Expression entryToInitValue; // Expression that yields value for entry in target tree. Expression expectedParamValue; // Expression that yield expectedType in target tree. ParameterExpression entryParameterForMembers; // Parameter expression members think of as "entry". ParameterExpression expectedParameterForMembers; // Parameter expression members think of as "expectedType" for entry. 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; // Annotate the entry expression with 'how we get to it' information. // The annotation on entryToInitiValue is picked up // The annotation on entryParameterForMembers is picked up to build nested member-init on entities. 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; // Here are the rewrites we do for member inits: // new T { id = t.id } // => ProjInit(pt, "id", f(t -> *.id)); // // new T { t2 = new T2 { id2 = *.t2.id2 } } // => ProjInit(pt, "t2", f(ProjInit(pt->t2), "id2", *.id2))) 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++); // Register the rewrite from the top to the entry if necessary. 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 { // We need an expression of object, which might require boxing. 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()); } // Revoke rewrites used for nested initialization. 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 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; }
/// <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; }
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); }