/// <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); }
/// <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)); }