コード例 #1
0
        public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs)
        {
            string propertyName      = null;
            string propertyTextValue = null;
            PropertySerializerEntry propertySerializerEntry = null;

            try
            {
                if (instance == null)
                {
                    instance = type.CreateInstance();
                }

                foreach (var pair in keyValuePairs)
                {
                    propertyName      = pair.Key;
                    propertyTextValue = pair.Value;

                    if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                    {
                        if (propertyName != "format" && propertyName != "callback" && propertyName != "debug")
                        {
                            Log.WarnFormat("Property '{0}' does not exist on type '{1}'", propertyName, type.FullName);
                        }
                        continue;
                    }

                    var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                    if (value == null)
                    {
                        Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                       instance, propertyName, propertyTextValue);
                        continue;
                    }
                    propertySerializerEntry.PropertySetFn(instance, value);
                }
                return(instance);
            }
            catch (Exception ex)
            {
                var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex);
                if (propertyName != null)
                {
                    serializationException.Data.Add("propertyName", propertyName);
                }
                if (propertyTextValue != null)
                {
                    serializationException.Data.Add("propertyValueString", propertyTextValue);
                }
                if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                {
                    serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType);
                }
                throw serializationException;
            }
        }
コード例 #2
0
        public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs)
        {
            string propertyName      = null;
            string propertyTextValue = null;
            PropertySerializerEntry propertySerializerEntry = null;

            if (instance == null)
            {
                instance = _CreateInstanceFn(type);
            }

            foreach (var pair in keyValuePairs)
            {
                propertyName      = pair.Key;
                propertyTextValue = pair.Value;

                if (string.IsNullOrEmpty(propertyTextValue))
                {
                    continue;
                }

                if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                {
                    if (propertyName == "v")
                    {
                        continue;
                    }

                    continue;
                }

                if (propertySerializerEntry.PropertySetFn == null)
                {
                    continue;
                }

                if (propertySerializerEntry.PropertyType == typeof(bool))
                {
                    //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
                    propertyTextValue = LeftPart(propertyTextValue, ',');
                }

                var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                if (value == null)
                {
                    continue;
                }
                propertySerializerEntry.PropertySetFn(instance, value);
            }

            return(instance);
        }
コード例 #3
0
        public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null)
#endif
        {
            string propertyName      = null;
            string propertyTextValue = null;
            PropertySerializerEntry propertySerializerEntry = null;

            try
            {
                if (instance == null)
                {
                    instance = type.CreateInstance();
                }

                foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
                {
                    propertyName      = pair.Key;
                    propertyTextValue = pair.Value;

                    if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                    {
                        var ignoredProperty = propertyName.ToLowerInvariant();
                        if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty))
                        {
                            Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName);
                        }
                        continue;
                    }

                    if (propertySerializerEntry.PropertySetFn == null)
                    {
                        Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName);
                        continue;
                    }

                    if (Type.GetTypeCode(propertySerializerEntry.PropertyType) == TypeCode.Boolean)
                    {
                        //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
                        propertyTextValue = propertyTextValue.SplitOnFirst(',').First();
                    }

                    var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                    if (value == null)
                    {
                        Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                       instance, propertyName, propertyTextValue);
                        continue;
                    }
                    propertySerializerEntry.PropertySetFn(instance, value);
                }
                return(instance);
            }
            catch (Exception ex)
            {
                var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex);
#if !NETCF
                if (propertyName != null)
                {
                    serializationException.Data.Add("propertyName", propertyName);
                }
                if (propertyTextValue != null)
                {
                    serializationException.Data.Add("propertyValueString", propertyTextValue);
                }
                if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                {
                    serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType);
                }
#endif
                throw serializationException;
            }
        }
コード例 #4
0
        public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null)
        {
            var errors = new List <RequestBindingError>();

            string propertyName      = null;
            string propertyTextValue = null;
            PropertySerializerEntry propertySerializerEntry = null;

            if (instance == null)
            {
                instance = type.CreateInstance();
            }

            foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
            {
                try
                {
                    propertyName      = pair.Key;
                    propertyTextValue = pair.Value;

                    if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                    {
                        if (propertyName == "v")
                        {
                            int version;
                            var hasVersion = instance as IHasVersion;
                            if (hasVersion != null && int.TryParse(pair.Value, out version))
                            {
                                hasVersion.Version = version;
                            }
                            continue;
                        }

                        var ignoredProperty = propertyName.ToLowerInvariant();
                        if (ignoredWarningsOnPropertyNames == null ||
                            !ignoredWarningsOnPropertyNames.Contains(ignoredProperty))
                        {
                            Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName);
                        }
                        continue;
                    }

                    if (propertySerializerEntry.PropertySetFn == null)
                    {
                        Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName,
                                       type.FullName);
                        continue;
                    }

                    if (propertySerializerEntry.PropertyType == typeof(bool))
                    {
                        //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
                        propertyTextValue = propertyTextValue.LeftPart(',');
                    }

                    var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                    if (value == null)
                    {
                        Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                       instance, propertyName, propertyTextValue);
                        continue;
                    }
                    propertySerializerEntry.PropertySetFn(instance, value);
                }
                catch (Exception ex)
                {
                    var error = new RequestBindingError {
                        ErrorMessage = ex.Message
                    };

                    if (propertyName != null)
                    {
                        error.PropertyName = propertyName;
                    }

                    if (propertyTextValue != null)
                    {
                        error.PropertyValueString = propertyTextValue;
                    }

                    if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                    {
                        error.PropertyType = propertySerializerEntry.PropertyType;
                    }

                    errors.Add(error);
                }
            }

            if (errors.Count > 0)
            {
                var serializationException = new SerializationException($"Unable to bind to request '{type.Name}'");
                serializationException.Data.Add("errors", errors);
                throw serializationException;
            }

            return(instance);
        }
コード例 #5
0
        private object PopulateFromKeyValue(object instance, string propertyName, string propertyTextValue, out PropertySerializerEntry propertySerializerEntry, List <RequestBindingError> errors, List <string> ignoredWarningsOnPropertyNames = null)
        {
            propertySerializerEntry = null;

            try
            {
                if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                {
                    if (propertyName == "v")
                    {
                        int version;
                        var hasVersion = instance as IHasVersion;
                        if (hasVersion != null && int.TryParse(propertyTextValue, out version))
                        {
                            hasVersion.Version = version;
                        }
                        return(instance);
                    }

                    var ignoredProperty = propertyName.ToLowerInvariant();
                    if (ignoredWarningsOnPropertyNames == null ||
                        !ignoredWarningsOnPropertyNames.Contains(ignoredProperty))
                    {
                        Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName);
                    }
                    return(instance);
                }

                if (propertySerializerEntry.PropertySetFn == null)
                {
                    Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName,
                                   type.FullName);
                    return(instance);
                }

                if (propertySerializerEntry.PropertyType == typeof(bool))
                {
                    //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
                    propertyTextValue = propertyTextValue.LeftPart(',');
                }

                var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                if (value == null)
                {
                    Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                   instance, propertyName, propertyTextValue);
                    return(instance);
                }
                propertySerializerEntry.PropertySetFn(instance, value);
            }
            catch (Exception ex)
            {
                var error = new RequestBindingError {
                    ErrorMessage = ex.Message
                };

                if (propertyName != null)
                {
                    error.PropertyName = propertyName;
                }

                if (propertyTextValue != null)
                {
                    error.PropertyValueString = propertyTextValue;
                }

                if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                {
                    error.PropertyType = propertySerializerEntry.PropertyType;
                }

                errors.Add(error);
            }

            return(instance);
        }
コード例 #6
0
        private object PopulateFromKeyValue(object instance, string propertyName, string propertyTextValue, out PropertySerializerEntry propertySerializerEntry,  List<RequestBindingError> errors, List<string> ignoredWarningsOnPropertyNames = null)
        {
            propertySerializerEntry = null;

            try
            {
                if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                {
                    if (propertyName == "v")
                    {
                        int version;
                        var hasVersion = instance as IHasVersion;
                        if (hasVersion != null && int.TryParse(propertyTextValue, out version))
                        {
                            hasVersion.Version = version;
                        }
                        return instance;
                    }

                    var ignoredProperty = propertyName.ToLowerInvariant();
                    if (ignoredWarningsOnPropertyNames == null ||
                        !ignoredWarningsOnPropertyNames.Contains(ignoredProperty))
                    {
                        Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName);
                    }
                    return instance;
                }

                if (propertySerializerEntry.PropertySetFn == null)
                {
                    Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName,
                                    type.FullName);
                    return instance;
                }

                if (propertySerializerEntry.PropertyType == typeof (bool))
                {
                    //InputExtensions.cs#530 MVC Checkbox helper emits extra hidden input field, generating 2 values, first is the real value
                    propertyTextValue = propertyTextValue.LeftPart(',');
                }

                var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                if (value == null)
                {
                    Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                    instance, propertyName, propertyTextValue);
                    return instance;
                }
                propertySerializerEntry.PropertySetFn(instance, value);
            }
            catch (Exception ex)
            {
                var error = new RequestBindingError { ErrorMessage = ex.Message };

                if (propertyName != null)
                    error.PropertyName = propertyName;

                if (propertyTextValue != null)
                    error.PropertyValueString = propertyTextValue;

                if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                    error.PropertyType = propertySerializerEntry.PropertyType;

                errors.Add(error);
            }

            return instance;
        }
コード例 #7
0
        public object PopulateFromMap(object instance, IDictionary <string, string> keyValuePairs, List <string> ignoredWarningsOnPropertyNames = null)
        {
            string propertyName      = null;
            string propertyTextValue = null;
            PropertySerializerEntry propertySerializerEntry = null;

            try
            {
                if (instance == null)
                {
                    instance = type.CreateInstance();
                }

                foreach (var pair in keyValuePairs.Where(x => !string.IsNullOrEmpty(x.Value)))
                {
                    propertyName      = pair.Key;
                    propertyTextValue = pair.Value;

                    if (!propertySetterMap.TryGetValue(propertyName, out propertySerializerEntry))
                    {
                        var ignoredProperty = propertyName.ToLowerInvariant();
                        if (ignoredWarningsOnPropertyNames == null || !ignoredWarningsOnPropertyNames.Contains(ignoredProperty))
                        {
                            Log.WarnFormat("Property '{0}' does not exist on type '{1}'", ignoredProperty, type.FullName);
                        }
                        continue;
                    }

                    if (propertySerializerEntry.PropertySetFn == null)
                    {
                        Log.WarnFormat("Could not set value of read-only property '{0}' on type '{1}'", propertyName, type.FullName);
                        continue;
                    }

                    var value = propertySerializerEntry.PropertyParseStringFn(propertyTextValue);
                    if (value == null)
                    {
                        Log.WarnFormat("Could not create instance on '{0}' for property '{1}' with text value '{2}'",
                                       instance, propertyName, propertyTextValue);
                        continue;
                    }
                    propertySerializerEntry.PropertySetFn(instance, value);
                }
                return(instance);
            }
            catch (Exception ex)
            {
                var serializationException = new SerializationException("KeyValueDataContractDeserializer: Error converting to type: " + ex.Message, ex);
                if (propertyName != null)
                {
                    serializationException.Data.Add("propertyName", propertyName);
                }
                if (propertyTextValue != null)
                {
                    serializationException.Data.Add("propertyValueString", propertyTextValue);
                }
                if (propertySerializerEntry != null && propertySerializerEntry.PropertyType != null)
                {
                    serializationException.Data.Add("propertyType", propertySerializerEntry.PropertyType);
                }
                throw serializationException;
            }
        }