public void ChangeTypeTest()
        {
            TestPrimitiveType value = (TestPrimitiveType)ClientConvert.ChangeType("Property_Value", typeof(TestPrimitiveType));

            Assert.AreEqual("Property_Value", value.Data);
            Assert.AreEqual(1, converter.ParseCall);
            Assert.AreEqual(0, converter.ToStringCall);
        }
        /// <summary>
        /// Converts a non-spatial primitive value to the target type.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type of the conversion.</param>
        /// <returns>The converted value.</returns>
        private static object ConvertNonSpatialValue(object value, Type targetType)
        {
            Debug.Assert(value != null, "value != null");

            // These types can be safely converted to directly, as there is no risk of precision being lost.
            if (CanSafelyConvertTo(targetType))
            {
                return(Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture));
            }

            string stringValue = ClientConvert.ToString(value);

            return(ClientConvert.ChangeType(stringValue, targetType));
        }
Esempio n. 3
0
        /// <summary>
        /// Converts a non-spatial primitive value to the target type.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <param name="targetType">The target type of the conversion.</param>
        /// <returns>The converted value.</returns>
        private static object ConvertNonSpatialValue(object value, Type targetType)
        {
            Debug.Assert(value != null, "value != null");
            TypeCode targetTypeCode = PlatformHelper.GetTypeCode(targetType);

            // These types can be safely converted to directly, as there is no risk of precision being lost.
            switch (targetTypeCode)
            {
            case TypeCode.Boolean:
            case TypeCode.Byte:
            case TypeCode.SByte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                return(Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture));
            }

            string stringValue = ClientConvert.ToString(value);

            return(ClientConvert.ChangeType(stringValue, targetType));
        }
Esempio n. 4
0
 /// <summary>
 /// Materialize by calling the Parse method on the converter
 /// </summary>
 /// <param name="clrType">clrType</param>
 /// <returns>A materialized instance</returns>
 internal override object Materialize(Type clrType)
 {
     return(ClientConvert.ChangeType(this.Text, clrType));
 }
Esempio n. 5
0
        /// <summary>
        /// Load property data form a raw response
        /// </summary>
        /// <param name="property">The property being loaded</param>
        /// <returns>property values as IEnumerable.</returns>
        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)
                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);

                        property.SetValue(this.entity, buffer, this.propertyName, false);
                    }
                    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);

                        property.SetValue(this.entity, convertedValue, this.propertyName, false);
                    }
                }

                if (property.MimeTypeProperty != null)
                {
                    // an implication of this 3rd-arg-null is that mime type properties cannot be open props
                    property.MimeTypeProperty.SetValue(this.entity, mimeType, null, false);
                }

                return(MaterializeAtom.CreateWrapper(context, results));
            }
            finally
            {
                context.ApplyingChanges = merging;
            }
        }