protected override void LeaveObject(IFieldSelection selection) { if (selection.Field.Member is PropertyInfo member) { SelectionClosure closure = Closures.Pop(); MemberInitExpression memberInit = closure.CreateMemberInit(); MemberExpression property = Expression.Property( Closures.Peek().Instance.Peek(), member); Expression withNullCheck = Expression.Condition( Expression.Equal(property, Expression.Constant(null)), Expression.Default(memberInit.Type), memberInit); Closures.Peek().Projections[selection.Field.Name] = Expression.Bind(selection.Field.Member, withNullCheck); base.LeaveObject(selection); } else { throw new QueryException( ErrorBuilder.New() .SetMessage( string.Format( "UseSelection is in a invalid state. Field {0}" + " should never have been visited!", selection.Field.Name)) .Build()); } }
protected override void LeaveLeaf(IFieldSelection selection) { if (selection.Field.Member is PropertyInfo member) { SelectionClosure closure = Closures.Peek(); closure.Projections[member.Name] = Expression.Bind( member, Expression.Property(closure.Instance.Peek(), member)); } base.LeaveLeaf(selection); }
protected override bool EnterObject(IFieldSelection selection) { if (selection.Field.Member is PropertyInfo property) { var nextClosure = new SelectionClosure( selection.Field.ClrType, "e" + Closures.Count); nextClosure.Instance.Push( Expression.Property( Closures.Peek().Instance.Peek(), property)); Closures.Push(nextClosure); return(base.EnterObject(selection)); } return(false); }
protected override void LeaveList(IFieldSelection selection) { if (selection.Field.Member is PropertyInfo propertyInfo) { SelectionClosure closure = Closures.Pop(); Expression body = Expression.Property( Closures.Peek().Instance.Peek(), propertyInfo); if (selection is FieldSelection fieldSelection) { var context = new SelectionVisitorContext( Context, _converter, fieldSelection, _selectionMiddlewareContext); for (var i = 0; i < _listHandler.Count; i++) { body = _listHandler[i].HandleLeave(context, selection, body); } } Expression select = closure.CreateSelection(body, propertyInfo.PropertyType); Closures.Peek().Projections[selection.Field.Name] = Expression.Bind(selection.Field.Member, select); base.LeaveList(selection); } else { throw new QueryException( ErrorBuilder.New() .SetMessage( string.Format( "UseSelection is in a invalid state. Field {0}" + " should never have been visited!", selection.Field.Name)) .Build()); } }