protected override Expression VisitMemberAccess(MemberExpression expression)
        {
            SPModelQueryExpressionScope currentScope = stack.Peek();

            switch (expression.Expression.NodeType)
            {
            case ExpressionType.Parameter:
            case ExpressionType.MemberAccess:
            case ExpressionType.Convert:
            case ExpressionType.Call:
                if (expression.Member.DeclaringType == typeof(ISPModelMetaData))
                {
                    switch (expression.Member.Name)
                    {
                    case "ID":
                        currentScope.Field = SPModelQueryFieldInfo.ID;
                        break;

                    case "UniqueId":
                        currentScope.Field = SPModelQueryFieldInfo.UniqueId;
                        break;

                    case "FileRef":
                        currentScope.Field = SPModelQueryFieldInfo.FileRef;
                        break;

                    case "FileLeafRef":
                        currentScope.Field = SPModelQueryFieldInfo.FileLeafRef;
                        break;

                    case "LastModified":
                        currentScope.Field = SPModelQueryFieldInfo.LastModified;
                        break;

                    case "CheckOutUserID":
                        currentScope.Field = SPModelQueryFieldInfo.CheckOutUserID;
                        break;

                    default:
                        throw new NotSupportedException(String.Format("The member '{0}' is not supported", expression.Member.Name));
                    }
                }
                else if (expression.Member.DeclaringType == typeof(TaxonomyItem) || expression.Member.DeclaringType == typeof(SPPrincipal))
                {
                    switch (expression.Member.Name)
                    {
                    case "Id":
                    case "ID":
                        Visit(expression.Expression);
                        break;

                    default:
                        throw new NotSupportedException(String.Format("The member '{0}' is not supported", expression.Member.Name));
                    }
                }
                else
                {
                    currentScope.MemberType        = expression.Type;
                    currentScope.Member            = expression.Member;
                    currentScope.FieldAssociations = SPModelFieldAssociationCollection.GetByMember(expression.Member);
                }
                break;

            default:
                throw new NotSupportedException(String.Format("The member '{0}' is not supported", expression.Member.Name));
            }
            return(expression);
        }
Exemple #2
0
        protected override Expression VisitMemberAccess(MemberExpression expression)
        {
            if (invariantExpression)
            {
                return(base.VisitMemberAccess(expression));
            }
            if (expression.Expression != lambdaParam)
            {
                if (expression.Member.DeclaringType == typeof(ISPModelMetaData) &&
                    ((expression.Expression.NodeType == ExpressionType.Call && ((MethodCallExpression)expression.Expression).Method == typeof(SPModelExtension).GetMethod("GetMetaData") && ((MethodCallExpression)expression.Expression).Arguments[0] == lambdaParam) ||
                     (expression.Expression.NodeType == ExpressionType.Convert && ((UnaryExpression)expression.Expression).Operand == lambdaParam)))
                {
                    // allow non-direct field access on the ISPModelMetaData interface
                }
                else
                {
                    Expression result = base.VisitMemberAccess(expression);
                    if (expression.Member.DeclaringType.IsGenericType && expression.Member.DeclaringType.GetGenericTypeDefinition() == typeof(Nullable <>) && expression.Member.Name == "HasValue")
                    {
                        CamlExpression expr = currentScope.GetExpression(v => Caml.IsNotNull(v.FieldRef));
                        currentScope.Reset();
                        currentScope.Expression = expr;
                    }
                    return(result);
                }
            }

            currentScope.MemberType        = expression.Type;
            currentScope.Member            = expression.Member;
            currentScope.Field             = default(SPModelQueryFieldInfo);
            currentScope.FieldAssociations = null;

            if (expression.Member.DeclaringType == typeof(ISPModelMetaData))
            {
                switch (expression.Member.Name)
                {
                case "ID":
                    currentScope.Field = SPModelQueryFieldInfo.ID;
                    break;

                case "UniqueId":
                    currentScope.Field = SPModelQueryFieldInfo.UniqueId;
                    break;

                case "FileRef":
                    currentScope.Field = SPModelQueryFieldInfo.FileRef;
                    break;

                case "FileLeafRef":
                    currentScope.Field = SPModelQueryFieldInfo.FileLeafRef;
                    break;

                case "LastModified":
                    currentScope.Field = SPModelQueryFieldInfo.LastModified;
                    break;

                case "CheckOutUserID":
                    currentScope.Field = SPModelQueryFieldInfo.CheckOutUserID;
                    break;

                default:
                    throw new NotSupportedException(String.Format("Member '{0}' is not supported", GetMemberFullName(expression.Member)));
                }
            }
            else
            {
                currentScope.FieldAssociations = SPModelFieldAssociationCollection.GetByMember(expression.Member);
                foreach (SPFieldAttribute field in currentScope.FieldAssociations.Fields)
                {
                    if (field.TypeAsString == "TaxonomyFieldType" || field.TypeAsString == "TaxonomyFieldTypeMulti")
                    {
                        builder.TaxonomyFields.Add(field.ListFieldInternalName);
                    }
                }
            }
            if (builder.SelectExpression != null)
            {
                if (currentScope.Field.FieldRef != null)
                {
                    builder.AddSelectProperty(currentScope.Field.FieldRef);
                }
                else if (currentScope.FieldAssociations.Queryable && expression.Member.MemberType == MemberTypes.Property && ((PropertyInfo)expression.Member).GetGetMethod().IsAbstract)
                {
                    builder.AddSelectProperty(currentScope.FieldAssociations.Fields.First().ListFieldInternalName);
                }
                else
                {
                    builder.SelectAllProperties = true;
                }
            }
            return(expression);
        }