using System; using System.Configuration; namespace ChadSoft { /// /// Class used for parsing strings to specific types. /// /// /// Original version copied (without permission) from Infragistics' web site code. /// Hopefully the lack of any type of real business logic will excuse this atrocity. /// If anyone is offended by this, feel free to let me know: jesschadwick@hotmail.com. /// public static class Parse { /// /// Gets a string showing the byte count at the most appropriate level of granularity (up to GB). /// /// Number of bytes. /// Formatted string for byte count. public static string FormatBytes(long byteCount) { string formatString; decimal output; if (byteCount < 1024) { formatString = "{0} Bytes"; output = byteCount; } else if (byteCount < 1048576) { formatString = "{0} KB"; output = Math.Round(byteCount / 1024M, 2); } else if (byteCount < 1073741824) { formatString = "{0} MB"; output = Math.Round(byteCount / 1048576M, 2); } else { formatString = "{0} GB"; output = Math.Round(byteCount / 1073741824M, 2); } return string.Format(formatString, output.ToString("n", System.Globalization.CultureInfo.CurrentUICulture.NumberFormat)); } /// /// Attempts to parse the given string to a 32-bit integer. /// /// Value to parse. /// Whether or not to throw an exception on failure. /// The parsed value or 0 if not parseable. /// Thrown if the value for inValue /// cannot be parsed as an integer and throwOnFailure is true. public static int ToInt32(string inValue, bool throwOnFailure) { int outValue = 0; if ((string.IsNullOrEmpty(inValue) || !int.TryParse(inValue, out outValue)) && throwOnFailure) CannotParse(inValue, "Int32"); return outValue; } /// /// Gets the specified setting from the app config and attempts to parse it to the specified type. /// /// Type to parse to. /// Name of config setting. /// Parsed setting or the default of T if could not parse. public static T AppSetting(string name) { return To(ConfigurationManager.AppSettings[name]); } /// /// Parses the given value, returning the default of T if it cannot parse. /// /// Type to parse to. /// Value to parse. /// Parsed value or the default of T. public static T To(string inValue) { return To(inValue, false); } /// /// Tries to parse the given value to the requested type. Throws an exception on failure /// if throwOnFailure is true; otherwise, returns the default value for T. /// /// Type to parse to. /// Value to parse. /// Whether or not to throw an exception on error. /// Parsed value or the default of T. public static T To(string inValue, bool throwOnFailure) { Type t = typeof(T); if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) { t = t.GetGenericArguments()[0]; } object outValue = null; if (t.IsEnum) { try { outValue = Enum.Parse(t, inValue, true); } catch (Exception ex) { if (throwOnFailure) CannotParse(inValue, t.Name, ex); } } else outValue = ByTypeCode(inValue, throwOnFailure, Type.GetTypeCode(t)); return outValue == null ? default(T) : (T)outValue; } /// /// Attempts to parse the given string to the requested type code. /// /// Value to parse. /// Whether or not to throw an exception on error. /// Type code of type to parse to. /// Parsed value or the default of T. public static object ByTypeCode(string inValue, bool throwOnFailure, TypeCode typeCode) { try { switch (typeCode) { case TypeCode.Boolean: inValue = inValue.ToLower(); switch (inValue) // this parses the stupid Ektron Yes/No values as bools.. { case "yes": return true; case "no": return false; case "1": return true; case "0": return false; default: return bool.Parse(inValue); } case TypeCode.Byte: return byte.Parse(inValue); case TypeCode.Char: return char.Parse(inValue); case TypeCode.DateTime: return DateTime.Parse(inValue); case TypeCode.Decimal: return decimal.Parse(inValue); case TypeCode.Double: return double.Parse(inValue); case TypeCode.Int16: return short.Parse(inValue); case TypeCode.Int32: return int.Parse(inValue); case TypeCode.Int64: return long.Parse(inValue); case TypeCode.SByte: return sbyte.Parse(inValue); case TypeCode.Single: return float.Parse(inValue); case TypeCode.String: return inValue; case TypeCode.UInt16: return ushort.Parse(inValue); case TypeCode.UInt32: return uint.Parse(inValue); case TypeCode.UInt64: return ulong.Parse(inValue); } } catch (Exception ex) { if (throwOnFailure) CannotParse(inValue, typeCode.ToString(), ex); } if (throwOnFailure) CannotParse(inValue, typeCode.ToString()); return null; } #region CannotParse static void CannotParse(string value, string targetType) { CannotParse(value, targetType, null); } static void CannotParse(string value, string targetType, Exception innerEx) { // LOCALIZE throw new FormatException(string.Format("Cannot parse value '{0}' to type '{1}'!", value, targetType), innerEx); } #endregion } }