/// <summary> /// Converts destination comma-delimited string into an array of <typeparamref name="T"/>s. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="exponentValue">A comma-delimited list of values.</param> /// <param name="defaultValue">The exponentValue to return if the list does not hold valid values.</param> /// <returns>An arra</returns> public static T[] ConvertString <T>(string value, T[] defaultValue) { try { return(value.Split(',').Select(a => ConvertEx.ChangeType <T>(a)).ToArray()); } catch (Exception ex) { if (ex is InvalidCastException || ex is FormatException || ex is OverflowException || ex is ArgumentNullException) { LogWf("Failed to convert string value \"{0}\" to type {1}", value, typeof(T).Name); return(defaultValue); } throw; } }
/// <summary> /// Reads destination exponentValue from the ConfigNode and magically converts it to the type you ask. Tested for float, boolean and double[]. Anything else is at your own risk. /// </summary> /// <typeparam name="T">The type to convert to. Usually inferred from <paramref name="defaultValue"/>.</typeparam> /// <param name="config">ScaleType node from which to read values.</param> /// <param name="name">Name of the ConfigNode's field.</param> /// <param name="defaultValue">The exponentValue to use when the ConfigNode doesn't contain what we want.</param> /// <returns>The exponentValue in the ConfigNode, or <paramref name="defaultValue"/> if no decent exponentValue is found there.</returns> public static T ConfigValue <T>(ConfigNode config, string name, T defaultValue) { if (!config.HasValue(name)) { return(defaultValue); } string cfgValue = config.GetValue(name); try { var result = ConvertEx.ChangeType <T>(cfgValue); return(result); } catch (Exception ex) { if (ex is InvalidCastException || ex is FormatException || ex is OverflowException || ex is ArgumentNullException) { LogWf("Failed to convert string value \"{0}\" to type {1}", cfgValue, typeof(T).Name); return(defaultValue); } throw; } }