Exemple #1
0
        private static object ParseProperty(PropertyInfo propertyInfo, KeyValuePair <string, string[]> keyValuePair)
        {
            if (keyValuePair.Equals(default(KeyValuePair <string, string[]>)) || keyValuePair.Value.Length == 0)
            {
                return(null);
            }

            return(ArrayToSpecifiedListParser.IsTypeValidForParsing(propertyInfo.PropertyType)
                                ? ArrayToSpecifiedListParser.ParseUndefined(keyValuePair.Value, propertyInfo.PropertyType, TryGetFormat(propertyInfo))
                                : StringToSpecifiedObjectParser.ParseUndefined(string.Join(",", keyValuePair.Value), propertyInfo.PropertyType, TryGetFormat(propertyInfo)));
        }
        /// <summary>
        /// Determines whether specified type is valid for parsing.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        /// <exception cref="ModelBindingException">Exception throws in case of underfined list type</exception>
        public static bool IsTypeValidForParsing(Type type)
        {
            if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(IList <>))
            {
                return(false);
            }

            var genericType = GetGenericListType(type);

            if (!StringToSpecifiedObjectParser.IsTypeValidForParsing(genericType))
            {
                throw new ModelBindingException(string.Format("Not supported list property type of: '{0}'", genericType));
            }

            return(true);
        }
        /// <summary>
        /// Parses the undefined values types from string array to list.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="type">The type.</param>
        /// <param name="format">The format.</param>
        /// <returns></returns>
        /// <exception cref="ModelBindingException"></exception>
        public static object ParseUndefined(string[] values, Type type, string format = null)
        {
            var parsingType = GetGenericListType(type);

            if (parsingType == typeof(string))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseString).ToList());
            }

            if (parsingType == typeof(int))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseInt).ToList());
            }

            if (parsingType == typeof(int?))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseNullableInt).ToList());
            }

            if (parsingType == typeof(bool))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseBool).ToList());
            }

            if (parsingType == typeof(bool?))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseNullableBool).ToList());
            }

            if (parsingType == typeof(decimal))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseDecimal).ToList());
            }

            if (parsingType == typeof(decimal?))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseNullableDecimal).ToList());
            }

            if (parsingType == typeof(long))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseLong).ToList());
            }

            if (parsingType == typeof(long?))
            {
                return(values.Select(StringToSpecifiedObjectParser.ParseNullableLong).ToList());
            }

            if (parsingType == typeof(DateTime))
            {
                return(values.Select(x => StringToSpecifiedObjectParser.ParseDateTime(x, format)).ToList());
            }

            if (parsingType == typeof(DateTime?))
            {
                return(values.Select(x => StringToSpecifiedObjectParser.ParseNullableDateTime(x, format)).ToList());
            }

            if (parsingType.IsEnum)
            {
                var listType   = typeof(List <>).MakeGenericType(parsingType);
                var list       = Activator.CreateInstance(listType);
                var methodInfo = listType.GetMethod("Add");

                foreach (var value in values)
                {
                    methodInfo.Invoke(list, new[] { StringToSpecifiedObjectParser.ParseEnum(value, parsingType) });
                }

                return(list);
            }

            throw new ModelBindingException(string.Format("Array parsing failed, not supported type: '{0}'", type));
        }