/// <summary> /// Converts an array value to a string of comma separated elements. /// </summary> /// <param name="value">The array value to convert.</param> /// <returns>A comma separated list of elements.</returns> private static String ConvertArrayToString(object value) { Type ltType = value.GetType(); if (!ltType.IsArray) { throw new ArgumentException("must be an array type", "value"); } StringBuilder lsbBuilder = new StringBuilder(); Type ltElementType = ltType.GetElementType(); bool lbFirst = true; foreach (Object loElement in (Array)value) { if (!lbFirst) { lsbBuilder.Append(','); } else { lbFirst = false; } lsbBuilder.Append(USENGConverter.ToString(ltElementType, loElement)); } return(lsbBuilder.ToString()); }
/// <summary> /// Converts a string to a data value of this type. /// </summary> /// <param name="value">The string value to convert.</param> /// <returns>The converted value or null on failure.</returns> public object ConvertFromString(string value) { try { if (BaseType.IsArray) { return(ConvertStringToArray(value, BaseType)); } else { return(USENGConverter.FromString(BaseType, value)); } } catch (Exception loE) { if (Logging.Enabled) { Logging.Log( this, String.Format( "Converting value '{0}' from string to '{1}' failed with exception: '{2}'", (value == null ? "(null)" : value), BaseType.ToString(), loE.ToString())); } return(null); } }
/// <summary> /// Converts a string of comma separated elements to an array value. /// </summary> /// <param name="values">The string of comma separated elements to convert.</param> /// <param name="arrayType">The array type to convert to.</param> /// <returns>A new object of type arrayType (empty array if no elements).</returns> private static Array ConvertStringToArray(string values, Type arrayType) { String[] lsValues = (values.Length == 0 ? new string[0] : values.Split(',')); Type ltElementType = arrayType.GetElementType(); Array laArray = Array.CreateInstance(ltElementType, lsValues.Length); int liIndex = 0; foreach (string lsValue in lsValues) { laArray.SetValue(USENGConverter.FromString(ltElementType, lsValue), liIndex); liIndex++; } return(laArray); }
/// <summary> /// Converts a data type value of this type to a string. /// </summary> /// <param name="value">The value to convert.</param> /// <returns>The string representation of the value or null if value is null.</returns> public string ConvertToString(object value) { if (value == null) { return(null); } else if (value != null && value.GetType().IsArray) { return(ConvertArrayToString(value)); } else { return(USENGConverter.ToString(BaseType, value)); } }