/// <summary>
        ///   Read type
        /// </summary>
        /// <returns></returns>
        public Type ReadType()
        {
            if (!_reader.ReadBoolean())
            {
                return(null);
            }
            string typeAsName = _reader.ReadString();

            return(_typeNameConverter.ConvertToType(typeAsName));
        }
Exemplo n.º 2
0
        //------------------------------------------------------------------------------
        public Type ConvertToType(string typeName)
        {
            if (String.IsNullOrEmpty(typeName))
            {
                return(null);
            }

            typeName = Regex.Replace(typeName,
                                     @"Advexp.Wrapper, Advexp.Settings.Utils.(PCL|Standard)",
                                     @"Advexp.Wrapper, Advexp.Settings.Utils");

            if (m_customTypeNameConverter == null)
            {
                return(GetDefaultTypeNameConverter().ConvertToType(typeName));
            }

            return(m_customTypeNameConverter.ConvertToType(typeName));
        }
Exemplo n.º 3
0
        /// <summary>
        ///   Reads attribute and converts it to type
        /// </summary>
        /// <param name = "attributeName"></param>
        /// <returns>null if nothing found</returns>
        public Type GetAttributeAsType(string attributeName)
        {
            string typeName = GetAttributeAsString(attributeName);

            return(_typeNameConverter.ConvertToType(typeName));
        }
Exemplo n.º 4
0
        /// <summary>
        /// </summary>
        /// <param name = "text"></param>
        /// <param name = "type">expected type. Result should be of this type.</param>
        /// <returns>null if the text is null</returns>
        public object ConvertFromString(string text, Type type)
        {
            try
            {
                if (type == typeof(string))
                {
                    return(text);
                }
                if (type == typeof(Boolean))
                {
                    return(Convert.ToBoolean(text, _cultureInfo));
                }
                if (type == typeof(Byte))
                {
                    return(Convert.ToByte(text, _cultureInfo));
                }
                if (type == typeof(Char))
                {
                    if (text == NullCharAsString)
                    {
                        // this is a null termination
                        return(NullChar);
                    }
                    //other chars
                    return(Convert.ToChar(text, _cultureInfo));
                }

                if (type == typeof(DateTime))
                {
                    return(Convert.ToDateTime(text, _cultureInfo));
                }
                if (type == typeof(Decimal))
                {
                    return(Convert.ToDecimal(text, _cultureInfo));
                }
                if (type == typeof(Double))
                {
                    return(Convert.ToDouble(text, _cultureInfo));
                }
                if (type == typeof(Int16))
                {
                    return(Convert.ToInt16(text, _cultureInfo));
                }
                if (type == typeof(Int32))
                {
                    return(Convert.ToInt32(text, _cultureInfo));
                }
                if (type == typeof(Int64))
                {
                    return(Convert.ToInt64(text, _cultureInfo));
                }
                if (type == typeof(SByte))
                {
                    return(Convert.ToSByte(text, _cultureInfo));
                }
                if (type == typeof(Single))
                {
                    return(Convert.ToSingle(text, _cultureInfo));
                }
                if (type == typeof(UInt16))
                {
                    return(Convert.ToUInt16(text, _cultureInfo));
                }
                if (type == typeof(UInt32))
                {
                    return(Convert.ToUInt32(text, _cultureInfo));
                }
                if (type == typeof(UInt64))
                {
                    return(Convert.ToUInt64(text, _cultureInfo));
                }

                if (type == typeof(TimeSpan))
                {
                    return(TimeSpan.Parse(text));
                }

                if (type == typeof(Guid))
                {
                    return(new Guid(text));
                }
                // Enumeration
                if (type.IsEnum)
                {
                    return(Enum.Parse(type, text, true));
                }
                // Array of byte
                if (type == typeof(byte[]))
                {
                    return(Convert.FromBase64String(text));
                }
                // Type-check must be last
                if (isType(type))
                {
                    return(_typeNameConverter.ConvertToType(text));
                }

                throw new InvalidOperationException(string.Format("Unknown simple type: {0}", type.FullName));
            }
            catch (Exception ex)
            {
                throw new SimpleValueParsingException(
                          string.Format("Invalid value: {0}. See details in the inner exception.", text), ex);
            }
        }