示例#1
0
        /// <summary>
        ///code originally in method ParseConstantFromElement(XElement xml, string elemName, Type expectedType)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public override bool TryDeserialize(XElement x, out Expression e)
        {
            if (x.Name.LocalName == typeof(ConstantExpression).Name)
            {
                Type serializedType = resolver.GetType(x.Element("Type").Value);
                if (serializedType != null && resolver.HasMappedKnownType(serializedType))
                {
                    string xml = x.Element("Value").Value;
                    DataContractSerializer dserializer;
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                    {
                        //if (typeof(IQueryable).IsAssignableFrom(expectedType) && IsIEnumerableOf(expectedType, knownType))
                        //{
                        //    dserializer = new DataContractSerializer(knownType.MakeArrayType(), this.resolver.knownTypes);
                        //    result = dserializer.ReadObject(ms);
                        //    result = Enumerable.ToArray(result);
                        //}
                        dserializer = new DataContractSerializer(serializedType, this.resolver.knownTypes);
                        object instance = dserializer.ReadObject(ms);
                        e = Expression.Constant(instance);
                        return(true);
                    }
                }
            }

            e = null;
            return(false);
        }
示例#2
0
        /// <summary>
        ///code originally in method ParseConstantFromElement(XElement xml, string elemName, Type expectedType)
        /// </summary>
        /// <param name="x"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public override bool TryDeserialize(XElement x, out Expression e)
        {
            if (x.Name.LocalName == typeof(ConstantExpression).Name)
            {
                String typeName = x.Element("Type").Value;
                if (String.IsNullOrEmpty(typeName))
                {
                    //added by leilei,for EnumType
                    typeName = ((XElement)(((XContainer)(x.Element("Type"))).FirstNode)).FirstAttribute.Value;
                    // typeName = ((XElement)(((XElement)(x.Element("Type").NextNode)).FirstNode)).FirstAttribute.Value;
                }
                Type serializedType = resolver.GetType(typeName);
                if (resolver.HasMappedKnownType(serializedType))
                {
                    string xml = x.Element("Value").Value;
                    DataContractSerializer dserializer;
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
                    {
                        //if (typeof(IQueryable).IsAssignableFrom(expectedType) && IsIEnumerableOf(expectedType, knownType))
                        //{
                        //    dserializer = new DataContractSerializer(knownType.MakeArrayType(), this.resolver.knownTypes);
                        //    result = dserializer.ReadObject(ms);
                        //    result = Enumerable.ToArray(result);
                        //}
                        dserializer = new DataContractSerializer(serializedType, this.resolver.knownTypes);
                        object instance = dserializer.ReadObject(ms);
                        e = Expression.Constant(instance);
                        return(true);
                    }
                }
            }

            e = null;
            return(false);
        }
示例#3
0
 public override bool TryDeserialize(XElement expressionXml, out Expression e)
 {
     if (expressionXml.Name.LocalName == "Table")
     {
         Type   type  = resolver.GetType(expressionXml.Attribute("Type").Value);
         ITable table = dc.GetTable(type);
         // REturning a random IQueryable of the right kind so that we can re-create the IQueryable
         // instance at the end of this method...
         QueryKind = table;
         e         = Expression.Constant(table);
         return(true);
     }
     e = null;
     return(false);
 }
        /// <summary>
        /// Upon deserialization replace the Query (IQueryable) in the Expression tree with a new Query that has a different ConstantExpression.
        /// Re-create the Query, but with a different server-side IQueryProvider.
        /// For IQueryProvder, we just create a Linq.EnumerableQuery`1.
        ///
        /// Need both a working IQueryProvider and a new Query with ConstantExpression equal to actual data.
        /// </summary>
        /// <param name="expressionXml"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public override bool TryDeserialize(XElement expressionXml, out Expression e)
        {
            if (this.creator == null || this.resolver == null)
            {
                throw new InvalidOperationException(string.Format("{0} and {1} instances are required for deserialization.", typeof(QueryCreator), typeof(TypeResolver)));
            }

            if (expressionXml.Name.LocalName == "Query")
            {
                Type    elementType = resolver.GetType(expressionXml.Attribute("elementType").Value);
                dynamic query       = creator.CreateQuery(elementType);
                e = Expression.Constant(query);
                return(true);
            }
            e = null;
            return(false);
        }