Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <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);
        }