コード例 #1
0
 private IEnumerable <MemberBinding> GetMemberBindingsForNewExpression(NewExpression newExpression)
 {
     for (int i = 0; i < newExpression.Members.Count; ++i)
     {
         yield return(MemberBinding.Bind(newExpression.Members[i], newExpression.Arguments[i]));
     }
 }
コード例 #2
0
        private IEnumerable <MemberBinding> GetMemberBindingsCreatedByExpression(Expression expression)
        {
            var memberInitExpression = expression as MemberInitExpression;

            if (memberInitExpression != null)
            {
                return(memberInitExpression.Bindings
                       .Where(binding => binding is MemberAssignment)
                       .Select(assignment => MemberBinding.Bind(assignment.Member, ((MemberAssignment)assignment).Expression)));
            }
            else
            {
                var newExpression = expression as NewExpression;
                if (newExpression != null && newExpression.Members != null)
                {
                    return(GetMemberBindingsForNewExpression(newExpression));
                }
                else
                {
                    return(null);
                }
            }
        }
コード例 #3
0
        protected override Expression VisitNewExpression(NewExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            // This ReSharper warning is wrong, expression.Members can be null.
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (expression.Members != null && expression.Members.Count > 0)
            // ReSharper restore ConditionIsAlwaysTrueOrFalse
            {
                // Scenario: (new X (A = arg1, B = arg2, ...)).Member - we can resolve this if one of (A, B, ...) matches Member.
                // Use the MemberBinding classes to determine this. (This takes care of A, B, ... and Member being  of different member types,
                // e.g., accessor MethodInfo and PropertyInfo.)
                var membersAndAssignedExpressions = expression.Members.Select((m, i) => MemberBinding.Bind(m, expression.Arguments[i]));
                var result = membersAndAssignedExpressions.SingleOrDefault(c => c.MatchesReadAccess(_memberInfo));

                if (result != null)
                {
                    // remove name if any - the name is only required at the definition, not at the reference
                    return(_context.RemoveNamesAndUpdateMapping(result.AssociatedExpression));
                }
            }

            // Scenario: (new X (A = arg1, B = arg2, ...)).Member - with a non-matching Member; or
            // Scenario: (new X (arg1, arg2, ...)).Member - we can't resolve this ATM


            throw new NotSupportedException(
                      string.Format(
                          "The member '{0}.{1}' cannot be translated to SQL. Expression: '{2}'",
                          expression.Type.Name,
                          _memberInfo.Name,
                          FormattingExpressionTreeVisitor.Format(expression)));
        }