public static NameValueCollection SetList <T>(this NameValueCollection q, string name, T[] values, bool throwExceptions, params int[] allowedSizes) where T : struct, IConvertible
        {
            if (values == null)
            {
                q.Remove(name);
                return(q);
            }
            bool flag = allowedSizes.Length == 0;

            foreach (int allowedSize in allowedSizes)
            {
                if (allowedSize == values.Length)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                if (throwExceptions)
                {
                    throw new ArgumentOutOfRangeException(nameof(values), "The specified array is not a valid length. Valid lengths are " + NameValueCollectionExtensions.JoinPrimitives <int>(allowedSizes, ','));
                }
                return(q);
            }
            q[name] = NameValueCollectionExtensions.JoinPrimitives <T>(values, ',');
            return(q);
        }
 /// <summary>
 /// Provides culture-invariant serialization of value types, in lower case for querystring readability. Setting a key to null removes it.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="q"></param>
 /// <param name="name"></param>
 /// <param name="val"></param>
 /// <returns></returns>
 public static NameValueCollection Set <T>(this NameValueCollection q, string name, T?val) where T : struct, IConvertible
 {
     if (!val.HasValue)
     {
         q.Remove(name);
     }
     else
     {
         q[name] = NameValueCollectionExtensions.SerializePrimitive <T>(val);
     }
     return(q);
 }
        private static string JoinPrimitives <T>(T[] array, char delimiter) where T : struct, IConvertible
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int index = 0; index < array.Length; ++index)
            {
                stringBuilder.Append(NameValueCollectionExtensions.SerializePrimitive <T>(new T?(array[index])));
                if (index < array.Length - 1)
                {
                    stringBuilder.Append(delimiter);
                }
            }
            return(stringBuilder.ToString());
        }
        /// <summary>
        /// Parses a comma-delimited list of primitive values. If there are unparsable items in the list, they will be replaced with 'fallbackValue'. If fallbackValue is null, the function will return null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <param name="fallbackValue"></param>
        /// <param name="allowedSizes"></param>
        /// <returns></returns>
        public static T[] ParseList <T>(string text, T?fallbackValue, params int[] allowedSizes) where T : struct, IConvertible
        {
            if (text == null)
            {
                return((T[])null);
            }
            text = text.Trim(' ', '(', ')', ',');
            if (text.Length == 0)
            {
                return((T[])null);
            }
            string[] strArray = text.Split(new char[1] {
                ','
            }, StringSplitOptions.None);
            bool flag = allowedSizes.Length == 0;

            foreach (int allowedSize in allowedSizes)
            {
                if (allowedSize == strArray.Length)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                return((T[])null);
            }
            T[] objArray = new T[strArray.Length];
            for (int index = 0; index < strArray.Length; ++index)
            {
                T?primitive = NameValueCollectionExtensions.ParsePrimitive <T>(strArray[index], fallbackValue);
                if (!primitive.HasValue)
                {
                    return((T[])null);
                }
                objArray[index] = primitive.Value;
            }
            return(objArray);
        }
 /// <summary>
 /// Provides culture-invariant parsing of int, double, float, bool, and enum values.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="q"></param>
 /// <param name="name"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static T Get <T>(this NameValueCollection q, string name, T defaultValue) where T : struct, IConvertible
 {
     return(NameValueCollectionExtensions.ParsePrimitive <T>(q[name], new T?(defaultValue)).Value);
 }
 public static T[] GetList <T>(this NameValueCollection q, string name, T?fallbackValue, params int[] allowedSizes) where T : struct, IConvertible
 {
     return(NameValueCollectionExtensions.ParseList <T>(q[name], fallbackValue, allowedSizes));
 }
예제 #7
0
 /// <summary>
 /// Provides culture-invariant serialization of value types, in lower case for querystring readability. Setting a key to null removes it.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="name"></param>
 /// <param name="val"></param>
 /// <returns></returns>
 public TK Set <T>(string name, T?val) where T : struct, IConvertible
 {
     return((TK)NameValueCollectionExtensions.Set <T>(this, name, val));
 }
예제 #8
0
 /// <summary>
 /// Serializes the given value by calling .ToString(). If the value is null, the key is removed.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="name"></param>
 /// <param name="val"></param>
 /// <returns></returns>
 public TK SetAsString <T>(string name, T val) where T : class
 {
     return((TK)NameValueCollectionExtensions.SetAsString <T>(this, name, val));
 }
예제 #9
0
 /// <summary>
 /// Provides culture-invariant parsing of byte, int, double, float, bool, and enum values.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="name"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public T?Get <T>(string name, T?defaultValue) where T : struct, IConvertible
 {
     return(NameValueCollectionExtensions.ParsePrimitive <T>(this[name], defaultValue));
 }