public override Expression VisitConstant(ConstantExpression exp)
 {
     if (exp.Value is ClientObjectCollection && exp.Value is IQueryable)
     {
         Type elementType = ExpressionUtility.GetElementType(exp.Value.GetType());
         Type type        = typeof(EnumerableClientObjectCollection <>).MakeGenericType(new Type[]
         {
             elementType
         });
         object obj = Activator.CreateInstance(type, new object[]
         {
             exp.Value
         });
         Type type2 = typeof(EnumerableQuery <>).MakeGenericType(new Type[]
         {
             elementType
         });
         object value = Activator.CreateInstance(type2, new object[]
         {
             obj
         });
         return(Expression.Constant(value));
     }
     return(base.VisitConstant(exp));
 }
        IQueryable IQueryProvider.CreateQuery(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            Type elementType = ExpressionUtility.GetElementType(expression.Type);

            if (elementType == null)
            {
                return(null);
            }
            IQueryable result;

            try
            {
                object obj = Activator.CreateInstance(typeof(ClientQueryable <>).MakeGenericType(new Type[]
                {
                    elementType
                }), new object[]
                {
                    this,
                    expression
                });
                result = (IQueryable)obj;
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
            return(result);
        }
Exemple #3
0
        internal static Type GetElementType(Type seqType)
        {
            Type type = ExpressionUtility.FindIEnumerable(seqType);

            if (type == null)
            {
                return(null);
            }
            //Edited for .NET Core
            //return type.GetGenericArguments()[0];
            return(type.GenericTypeArguments[0]);
        }
        private static void CheckSelectExpression(MethodCallExpression exp)
        {
            if (exp.Arguments.Count != 2)
            {
                throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
            }
            Expression       expression       = ExpressionUtility.StripQuotes(exp.Arguments[1]);
            LambdaExpression lambdaExpression = expression as LambdaExpression;

            if (lambdaExpression == null || lambdaExpression.Parameters.Count != 1)
            {
                throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
            }
            Expression          expression2         = ExpressionUtility.StripConverts(ExpressionUtility.StripQuotes(lambdaExpression.Body));
            ParameterExpression parameterExpression = expression2 as ParameterExpression;

            if (parameterExpression == null || parameterExpression.Name != lambdaExpression.Parameters[0].Name)
            {
                throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
            }
        }
Exemple #5
0
        public static void WriteValueToXmlElement(XmlWriter writer, object objValue, SerializationContext serializationContext)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (serializationContext == null)
            {
                throw new ArgumentNullException("serializationContext");
            }
            if (objValue == null)
            {
                writer.WriteAttributeString("Type", "Null");
                return;
            }
            if (!(objValue is ClientObject))
            {
                if (objValue is ClientValueObject)
                {
                    ClientValueObject clientValueObject = (ClientValueObject)objValue;
                    writer.WriteAttributeString("TypeId", clientValueObject.TypeId);
                    if (!clientValueObject.CustomWriteToXml(writer, serializationContext))
                    {
                        clientValueObject.WriteToXml(writer, serializationContext);
                        return;
                    }
                }
                else
                {
                    if (objValue is Stream)
                    {
                        Stream stream = (Stream)objValue;
                        writer.WriteAttributeString("Type", "Binary");
                        writer.WriteStartElement(MtomGlobals.XopIncludeLocalName);
                        string text = "http://sharepoint.microsoft.com/" + serializationContext.Context.NextSequenceId.ToString(CultureInfo.InvariantCulture);
                        serializationContext.AddStream(new StreamInfo(text, stream));
                        writer.WriteAttributeString(MtomGlobals.XopIncludeHrefLocalName, "cid:" + text);
                        writer.WriteEndElement();
                        return;
                    }
                    DataConvert.WriteValueHandler writeValueHandler = null;
                    if (DataConvert.s_writeValueHandlers.TryGetValue(objValue.GetType(), out writeValueHandler))
                    {
                        writer.WriteAttributeString("Type", DataConvert.s_writeValueTypeNames[objValue.GetType()]);
                        writeValueHandler(writer, objValue);
                        return;
                    }
                    if (objValue.GetType().IsArray)
                    {
                        writer.WriteAttributeString("Type", "Array");
                        //Edited for .NET Core
                        //using (IEnumerator enumerator = ((Array)objValue).GetEnumerator())
                        IEnumerator enumerator = ((Array)objValue).GetEnumerator();
                        {
                            while (enumerator.MoveNext())
                            {
                                object current = enumerator.Current;
                                writer.WriteStartElement("Object");
                                DataConvert.WriteValueToXmlElement(writer, current, serializationContext);
                                writer.WriteEndElement();
                            }
                            return;
                        }
                    }
                    if (objValue is IDictionary)
                    {
                        writer.WriteAttributeString("Type", "Dictionary");
                        IDictionary           dictionary  = objValue as IDictionary;
                        IDictionaryEnumerator enumerator2 = dictionary.GetEnumerator();
                        while (enumerator2.MoveNext())
                        {
                            object value = enumerator2.Value;
                            if (DataConvert.CouldSerializeDictionaryEntry(value) && enumerator2.Key is string)
                            {
                                writer.WriteStartElement("Property");
                                writer.WriteAttributeString("Name", (string)enumerator2.Key);
                                DataConvert.WriteValueToXmlElement(writer, enumerator2.Value, serializationContext);
                                writer.WriteEndElement();
                            }
                        }
                        return;
                    }
                    //Edited for .NET Core
                    //if (typeof(IEnumerable).IsAssignableFrom(objValue.GetType()) && ExpressionUtility.GetElementType(objValue.GetType()) != null)

                    if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(objValue.GetType()) && ExpressionUtility.GetElementType(objValue.GetType()) != null)
                    {
                        writer.WriteAttributeString("Type", "Array");
                        //Edited for .NET Core
                        //using (IEnumerator enumerator3 = ((IEnumerable)objValue).GetEnumerator())
                        IEnumerator enumerator3 = ((IEnumerable)objValue).GetEnumerator();
                        {
                            while (enumerator3.MoveNext())
                            {
                                object current2 = enumerator3.Current;
                                writer.WriteStartElement("Object");
                                DataConvert.WriteValueToXmlElement(writer, current2, serializationContext);
                                writer.WriteEndElement();
                            }
                            return;
                        }
                    }
                    Type type = objValue.GetType();
                    //Edited for .NET Core
                    //if (type.IsEnum)
                    if (type.GetTypeInfo().IsEnum)
                    {
                        writer.WriteAttributeString("Type", "Enum");
                        DataConvert.s_writeValueEnum(writer, objValue);
                        return;
                    }
                    writer.WriteAttributeString("Type", "Unspecified");
                    writer.WriteString(objValue.ToString());
                }
                return;
            }
            ClientObject clientObject = (ClientObject)objValue;

            if (clientObject.Path == null)
            {
                throw new ClientRequestException(Resources.GetString("NoObjectPathAssociatedWithObject"));
            }
            ClientAction.CheckActionParameterInContext(serializationContext.Context, clientObject);
            writer.WriteAttributeString("ObjectPathId", clientObject.Path.Id.ToString(CultureInfo.InvariantCulture));
            serializationContext.AddClientObject(clientObject);
        }
Exemple #6
0
        private static Type FindIEnumerable(Type seqType)
        {
            if (seqType == null || seqType == typeof(string))
            {
                return(null);
            }
            if (seqType.IsArray)
            {
                return(typeof(IEnumerable <>).MakeGenericType(new Type[]
                {
                    seqType.GetElementType()
                }));
            }
            //Edited for .NET Core
            //if (seqType.IsGenericType)
            if (seqType.GetTypeInfo().IsGenericType)
            {
                //Edited for .NET Core
                //Type[] genericArguments = seqType.GetGenericArguments();
                Type[] genericArguments = seqType.GenericTypeArguments.ToArray();
                for (int i = 0; i < genericArguments.Length; i++)
                {
                    Type type  = genericArguments[i];
                    Type type2 = typeof(IEnumerable <>).MakeGenericType(new Type[]
                    {
                        type
                    });
                    //Edited for .NET Core
                    //if (type2.IsAssignableFrom(seqType))

                    if (type2.GetTypeInfo().IsAssignableFrom(seqType))
                    {
                        Type result = type2;
                        return(result);
                    }
                }
            }
            //Edited for .NET Core
            //Type[] interfaces = seqType.GetInterfaces();
            Type[] interfaces = seqType.GetTypeInfo().GetInterfaces();
            if (interfaces != null && interfaces.Length > 0)
            {
                Type[] array = interfaces;
                for (int j = 0; j < array.Length; j++)
                {
                    Type seqType2 = array[j];
                    Type type3    = ExpressionUtility.FindIEnumerable(seqType2);
                    if (type3 != null)
                    {
                        Type result = type3;
                        return(result);
                    }
                }
            }
            //Edited for .NET Core
            //if (seqType.BaseType != null && seqType.BaseType != typeof(object))
            if (seqType.GetTypeInfo().BaseType != null && seqType.GetTypeInfo().BaseType != typeof(object))
            {
                //Edited for .NET Core
                //return ExpressionUtility.FindIEnumerable(seqType.BaseType);
                return(ExpressionUtility.FindIEnumerable(seqType.GetTypeInfo().BaseType));
            }
            return(null);
        }
        private static ClientQueryInternal ProcessClientQueryableMethodCallQueryExpression(
            ClientQueryInternal rootQuery, ClientObject rootClientObject,
            MethodCallExpression exp, bool leaf,
            DataRetrieval.QueryProcessInfo queryInfo, DataRetrieval.QueryMethodAggregator aggregator)
        {
            //Edited for .NET Core
            string name;

            if ((name = exp.Method.Name) != null)
            {
                //Edited for .NET Core
                // This was bugged when reflecting the source, wo it needs checking
                var newDictionary = new Dictionary <string, int>(8)
                {
                    {
                        "Where",
                        0
                    },
                    {
                        "OrderBy",
                        1
                    },
                    {
                        "OrderByDescending",
                        2
                    },
                    {
                        "ThenBy",
                        3
                    },
                    {
                        "ThenByDescending",
                        4
                    },
                    {
                        "Select",
                        5
                    },
                    {
                        "Take",
                        6
                    },
                    {
                        "OfType",
                        7
                    }
                };
                int num;
                if (newDictionary.TryGetValue(name, out num))
                {
                    ClientQueryInternal result;
                    switch (num)
                    {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    {
                        ClientQueryInternal clientQueryInternal = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Arguments[0], leaf, queryInfo, aggregator);
                        string name2;
                        if ((name2 = exp.Method.Name) != null)
                        {
                            if (!(name2 == "Where"))
                            {
                                if (!(name2 == "OrderBy"))
                                {
                                    if (name2 == "OrderByDescending")
                                    {
                                        aggregator.OrderByDescending++;
                                    }
                                }
                                else
                                {
                                    aggregator.OrderBy++;
                                }
                            }
                            else
                            {
                                aggregator.Where++;
                            }
                        }
                        Expression       expression       = ExpressionUtility.StripQuotes(exp.Arguments[1]);
                        LambdaExpression lambdaExpression = expression as LambdaExpression;
                        if (lambdaExpression == null)
                        {
                            throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                        }
                        ChunkStringBuilder chunkStringBuilder = new ChunkStringBuilder();
                        XmlWriter          xmlWriter          = chunkStringBuilder.CreateXmlWriter();
                        string             name3;
                        if ((name3 = exp.Method.Name) != null)
                        {
                            if (!(name3 == "Where"))
                            {
                                if (!(name3 == "OrderBy"))
                                {
                                    if (!(name3 == "OrderByDescending"))
                                    {
                                        if (!(name3 == "ThenBy"))
                                        {
                                            if (!(name3 == "ThenByDescending"))
                                            {
                                                goto IL_224;
                                            }
                                            xmlWriter.WriteStartElement("ThenByDescending");
                                        }
                                        else
                                        {
                                            xmlWriter.WriteStartElement("ThenBy");
                                        }
                                    }
                                    else
                                    {
                                        xmlWriter.WriteStartElement("OrderByDescending");
                                    }
                                }
                                else
                                {
                                    xmlWriter.WriteStartElement("OrderBy");
                                }
                            }
                            else
                            {
                                xmlWriter.WriteStartElement("Where");
                            }
                            ClientQueryInternal.WriteFilterExpressionToXml(xmlWriter, clientQueryInternal.ChildItemQuery.ChildItemExpressionSerializationContext, lambdaExpression);
                            xmlWriter.WriteStartElement("Object");
                            ChunkStringBuilder chunkStringBuilder2 = null;
                            if (queryInfo.Expression.TryGetValue(clientQueryInternal.ChildItemQuery, out chunkStringBuilder2))
                            {
                                chunkStringBuilder2.WriteContentAsRawXml(xmlWriter);
                            }
                            else
                            {
                                xmlWriter.WriteStartElement("QueryableObject");
                                xmlWriter.WriteEndElement();
                            }
                            xmlWriter.WriteEndElement();
                            xmlWriter.WriteEndElement();
                            xmlWriter.Dispose();
                            queryInfo.Expression[clientQueryInternal.ChildItemQuery] = chunkStringBuilder;
                            result = clientQueryInternal;
                            break;
                        }
IL_224:
                        throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                    }

                    case 5:
                    {
                        DataRetrieval.CheckSelectExpression(exp);
                        ClientQueryInternal clientQueryInternal2 = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Arguments[0], leaf, queryInfo, aggregator);
                        aggregator.Select++;
                        result = clientQueryInternal2;
                        queryInfo.BySelect[clientQueryInternal2.ChildItemQuery.Id] = clientQueryInternal2.ChildItemQuery;
                        break;
                    }

                    case 6:
                    {
                        ClientQueryInternal clientQueryInternal3 = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Arguments[0], leaf, queryInfo, aggregator);
                        aggregator.Take++;
                        Expression expression2 = exp.Arguments[1];
                        expression2 = ExpressionEvaluator.PartialEvaluate(expression2, new Func <Expression, bool>(DataRetrieval.ExpectConstantExpression_CanExpressionBeEvaluated));
                        if (expression2.NodeType != ExpressionType.Constant || expression2.Type != typeof(int))
                        {
                            throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                        }
                        ConstantExpression constantExpression = (ConstantExpression)expression2;
                        int num2 = (int)constantExpression.Value;
                        if (num2 < 0)
                        {
                            throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                        }
                        ChunkStringBuilder chunkStringBuilder3 = new ChunkStringBuilder();
                        XmlWriter          xmlWriter2          = chunkStringBuilder3.CreateXmlWriter();
                        xmlWriter2.WriteStartElement("Take");
                        xmlWriter2.WriteAttributeString("Count", num2.ToString(CultureInfo.InvariantCulture));
                        ChunkStringBuilder chunkStringBuilder4 = null;
                        if (queryInfo.Expression.TryGetValue(clientQueryInternal3.ChildItemQuery, out chunkStringBuilder4))
                        {
                            chunkStringBuilder4.WriteContentAsRawXml(xmlWriter2);
                        }
                        else
                        {
                            xmlWriter2.WriteStartElement("QueryableObject");
                            xmlWriter2.WriteEndElement();
                        }
                        xmlWriter2.WriteEndElement();
                        xmlWriter2.Dispose();
                        queryInfo.Expression[clientQueryInternal3.ChildItemQuery] = chunkStringBuilder3;
                        result = clientQueryInternal3;
                        break;
                    }

                    case 7:
                    {
                        ClientQueryInternal clientQueryInternal4 = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Arguments[0], leaf, queryInfo, aggregator);
                        if (!exp.Method.IsGenericMethod || exp.Method.ContainsGenericParameters)
                        {
                            throw new InvalidQueryExpressionException(Resources.GetString("NotSupportedQueryExpressionWithExpressionValue", new object[]
                                {
                                    exp.ToString()
                                }));
                        }
                        Type[] genericArguments = exp.Method.GetGenericArguments();
                        if (genericArguments == null || genericArguments.Length != 1)
                        {
                            throw new InvalidQueryExpressionException(Resources.GetString("NotSupportedQueryExpressionWithExpressionValue", new object[]
                                {
                                    exp.ToString()
                                }));
                        }
                        string value  = null;
                        string value2 = null;
                        DataConvert.GetTypeNameOrTypeId(genericArguments[0], out value, out value2);
                        if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(value2))
                        {
                            throw new InvalidQueryExpressionException(Resources.GetString("NotSupportedQueryExpressionWithExpressionValue", new object[]
                                {
                                    exp.ToString()
                                }));
                        }
                        ChunkStringBuilder chunkStringBuilder5 = new ChunkStringBuilder();
                        XmlWriter          xmlWriter3          = chunkStringBuilder5.CreateXmlWriter();
                        xmlWriter3.WriteStartElement("OfType");
                        if (!string.IsNullOrEmpty(value))
                        {
                            xmlWriter3.WriteAttributeString("Type", value);
                        }
                        else
                        {
                            xmlWriter3.WriteAttributeString("TypeId", value2);
                        }
                        ChunkStringBuilder chunkStringBuilder6 = null;
                        if (queryInfo.Expression.TryGetValue(clientQueryInternal4.ChildItemQuery, out chunkStringBuilder6))
                        {
                            chunkStringBuilder6.WriteContentAsRawXml(xmlWriter3);
                        }
                        else
                        {
                            xmlWriter3.WriteStartElement("QueryableObject");
                            xmlWriter3.WriteEndElement();
                        }
                        xmlWriter3.WriteEndElement();
                        xmlWriter3.Dispose();
                        queryInfo.Expression[clientQueryInternal4.ChildItemQuery] = chunkStringBuilder5;
                        result = clientQueryInternal4;
                        break;
                    }

                    default:
                        goto IL_5B7;
                    }
                    return(result);
                }
            }
IL_5B7:
            throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
        }
        private static ClientQueryInternal ProcessMethodCallQueryExpression(ClientQueryInternal rootQuery, ClientObject rootClientObject, MethodCallExpression exp, bool leaf, DataRetrieval.QueryProcessInfo queryInfo, DataRetrieval.QueryMethodAggregator aggregator)
        {
            ClientQueryInternal result;

            if (exp.Method.IsGenericMethod && (exp.Method.DeclaringType == typeof(Enumerable) || exp.Method.DeclaringType == typeof(System.Linq.Queryable)))
            {
                result = DataRetrieval.ProcessClientQueryableMethodCallQueryExpression(rootQuery, rootClientObject, exp, leaf, queryInfo, aggregator);
            }
            else if (exp.Method.IsGenericMethod && exp.Method.DeclaringType == typeof(ClientObjectQueryableExtension) && (exp.Method.Name == "Include" || exp.Method.Name == "IncludeWithDefaultProperties"))
            {
                if (exp.Arguments.Count != 2)
                {
                    throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                }
                IEnumerable <Expression> enumerable         = null;
                NewArrayExpression       newArrayExpression = exp.Arguments[1] as NewArrayExpression;
                if (newArrayExpression == null)
                {
                    Expression expression = ExpressionEvaluator.PartialEvaluate(exp.Arguments[1], new Func <Expression, bool>(DataRetrieval.ExpectConstantExpression_CanExpressionBeEvaluated));
                    if (expression.NodeType == ExpressionType.Constant)
                    {
                        ConstantExpression constantExpression = (ConstantExpression)expression;
                        enumerable = (constantExpression.Value as IEnumerable <Expression>);
                    }
                    if (enumerable == null)
                    {
                        throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                    }
                }
                else
                {
                    enumerable = newArrayExpression.Expressions;
                }
                ClientQueryInternal clientQueryInternal = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Arguments[0], false, queryInfo, aggregator);
                aggregator.Include++;
                if (clientQueryInternal == null)
                {
                    throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                }
                queryInfo.ByInclude[clientQueryInternal.ChildItemQuery.Id] = clientQueryInternal.ChildItemQuery;
                ClientQueryInternal childItemQuery = clientQueryInternal.ChildItemQuery;
                if (exp.Method.Name == "IncludeWithDefaultProperties")
                {
                    childItemQuery.SelectAllProperties();
                }
                foreach (Expression current in enumerable)
                {
                    Expression       expression2      = ExpressionUtility.StripQuotes(current);
                    LambdaExpression lambdaExpression = expression2 as LambdaExpression;
                    if (lambdaExpression == null)
                    {
                        expression2 = ExpressionEvaluator.PartialEvaluate(expression2, new Func <Expression, bool>(DataRetrieval.ExpectConstantExpression_CanExpressionBeEvaluated));
                        ConstantExpression constantExpression2 = expression2 as ConstantExpression;
                        if (constantExpression2 != null)
                        {
                            lambdaExpression = (constantExpression2.Value as LambdaExpression);
                        }
                        if (lambdaExpression == null)
                        {
                            throw DataRetrieval.CreateInvalidQueryExpressionException(expression2);
                        }
                    }
                    Expression exp2 = lambdaExpression.Body;
                    exp2 = ExpressionUtility.StripConverts(exp2);
                    DataRetrieval.QueryMethodAggregator queryMethodAggregator = new DataRetrieval.QueryMethodAggregator(current);
                    DataRetrieval.ProcessQueryExpression(childItemQuery, null, exp2, true, queryInfo, queryMethodAggregator);
                    queryMethodAggregator.Check();
                }
                result = clientQueryInternal;
            }
            else
            {
                if (!ExpressionUtility.IsGetFieldValueMethod(exp.Method))
                {
                    throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                }
                ClientQueryInternal clientQueryInternal2 = DataRetrieval.ProcessQueryExpression(rootQuery, rootClientObject, exp.Object, false, queryInfo, aggregator);
                aggregator.Member++;
                if (clientQueryInternal2 == null)
                {
                    throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                }
                Expression expression3 = exp.Arguments[0];
                expression3 = ExpressionEvaluator.PartialEvaluate(expression3, new Func <Expression, bool>(DataRetrieval.ExpectConstantExpression_CanExpressionBeEvaluated));
                if (expression3.NodeType != ExpressionType.Constant || expression3.Type != typeof(string))
                {
                    throw DataRetrieval.CreateInvalidQueryExpressionException(exp);
                }
                ConstantExpression constantExpression3 = (ConstantExpression)expression3;
                clientQueryInternal2.Select((string)constantExpression3.Value);
                result = null;
            }
            return(result);
        }