internal override object Materialize(Type clrType)
 {
     return(ClientConvert.ChangeType(this.Text, clrType));
 }
Exemplo n.º 2
0
        private MaterializeAtom ReadPropertyFromRawData(ClientPropertyAnnotation property)
        {
            DataServiceContext context = (DataServiceContext)this.Source;

            bool merging = context.ApplyingChanges;

            try
            {
                context.ApplyingChanges = true;

                // if this is the data property for a media entry, what comes back
                // is the raw value (no markup)
#if ASTORIA_OPEN_OBJECT
                object openProps = null;
#endif
                string   mimeType    = null;
                Encoding encoding    = null;
                Type     elementType = property.EntityCollectionItemType ?? property.NullablePropertyType;
                IList    results     = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType));
                ContentTypeUtil.ReadContentType(this.ContentType, out mimeType, out encoding);

                using (Stream responseStream = this.GetResponseStream())
                {
                    // special case byte[], and for everything else let std conversion kick-in
                    if (property.PropertyType == typeof(byte[]))
                    {
                        int    total  = checked ((int)this.ContentLength);
                        byte[] buffer = null;
                        if (total >= 0)
                        {
                            buffer = LoadPropertyResult.ReadByteArrayWithContentLength(responseStream, total);
                        }
                        else
                        {
                            buffer = LoadPropertyResult.ReadByteArrayChunked(responseStream);
                        }

                        results.Add(buffer);
#if ASTORIA_OPEN_OBJECT
                        property.SetValue(this.entity, buffer, this.propertyName, ref openProps, false);
#else
                        property.SetValue(this.entity, buffer, this.propertyName, false);
#endif
                    }
                    else
                    {
                        // responseStream will disposed, StreamReader doesn't need to dispose of it.
                        StreamReader reader         = new StreamReader(responseStream, encoding);
                        object       convertedValue = property.PropertyType == typeof(string) ?
                                                      reader.ReadToEnd() :
                                                      ClientConvert.ChangeType(reader.ReadToEnd(), property.PropertyType);
                        results.Add(convertedValue);
#if ASTORIA_OPEN_OBJECT
                        property.SetValue(this.entity, convertedValue, this.propertyName, ref openProps, false);
#else
                        property.SetValue(this.entity, convertedValue, this.propertyName, false);
#endif
                    }
                }

#if ASTORIA_OPEN_OBJECT
                Debug.Assert(openProps == null, "These should not be set in this path");
#endif
                if (property.MimeTypeProperty != null)
                {
                    // an implication of this 3rd-arg-null is that mime type properties cannot be open props
#if ASTORIA_OPEN_OBJECT
                    property.MimeTypeProperty.SetValue(this.entity, mimeType, null, ref openProps, false);
                    Debug.Assert(openProps == null, "These should not be set in this path");
#else
                    property.MimeTypeProperty.SetValue(this.entity, mimeType, null, false);
#endif
                }

                return(MaterializeAtom.CreateWrapper(context, results));
            }
            finally
            {
                context.ApplyingChanges = merging;
            }
        }
Exemplo n.º 3
0
        private MaterializeAtom ReadPropertyFromRawData(ClientPropertyAnnotation property)
        {
            MaterializeAtom    atom;
            DataServiceContext source = (DataServiceContext)base.Source;
            bool applyingChanges      = source.ApplyingChanges;

            try
            {
                source.ApplyingChanges = true;
                string   mime     = null;
                Encoding encoding = null;
                Type     type     = property.EntityCollectionItemType ?? property.NullablePropertyType;
                IList    results  = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(new Type[] { type }));
                HttpProcessUtility.ReadContentType(base.ContentType, out mime, out encoding);
                using (Stream stream = base.GetResponseStream())
                {
                    if (property.PropertyType == typeof(byte[]))
                    {
                        int    contentLength = (int)base.ContentLength;
                        byte[] buffer        = null;
                        if (contentLength >= 0)
                        {
                            buffer = ReadByteArrayWithContentLength(stream, contentLength);
                        }
                        else
                        {
                            buffer = ReadByteArrayChunked(stream);
                        }
                        results.Add(buffer);
                        property.SetValue(this.entity, buffer, this.propertyName, false);
                    }
                    else
                    {
                        StreamReader reader = new StreamReader(stream, encoding);
                        object       obj2   = (property.PropertyType == typeof(string)) ? reader.ReadToEnd() : ClientConvert.ChangeType(reader.ReadToEnd(), property.PropertyType);
                        results.Add(obj2);
                        property.SetValue(this.entity, obj2, this.propertyName, false);
                    }
                }
                if (property.MimeTypeProperty != null)
                {
                    property.MimeTypeProperty.SetValue(this.entity, mime, null, false);
                }
                atom = MaterializeAtom.CreateWrapper(source, results);
            }
            finally
            {
                source.ApplyingChanges = applyingChanges;
            }
            return(atom);
        }