Exemplo n.º 1
0
        /// <summary>
        /// Gets the pattern for the specified type.
        /// </summary>
        /// <param name="type">The value type.</param>
        /// <returns>The pattern.</returns>
        public override string GetPattern(Type type)
        {
            if (TypeExtensions.IsNullable(type))
            {
                return(@"(?<{0}>(?:-?\d+)|(?:null))(?:st|nd|rd|th)?");
            }

            return(@"(?<{0}>-?\d+)(?:st|nd|rd|th)?");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the pattern for the specified type.
        /// </summary>
        /// <param name="type">The value type.</param>
        /// <returns>The pattern.</returns>
        public override string GetPattern(Type type)
        {
            if (TypeExtensions.IsNullable(type))
            {
                return(@"(?:\$\s*)?(?<{0}>(?:-?\d+(?:\.\d+)?)|(?:null))");
            }

            return(@"(?:\$\s*)?(?<{0}>-?\d+(?:\.\d+)?)");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <returns>The actual value.</returns>
        public static object ParseValue(string value, Type type)
        {
            if (value == null)
            {
                if (type.IsValueType)
                {
                    return(Activator.CreateInstance(type));
                }

                return(null);
            }

            if (TypeExtensions.IsNullable(type))
            {
                if (value == "null")
                {
                    return(null);
                }

                type = Nullable.GetUnderlyingType(type);
            }

            if (type.IsEnum)
            {
                return(Enum.Parse(type, NameComparer.NormalizeName(value), true));
            }

            if (type == typeof(int))
            {
                return(Int32Parser.ParseInt32(value));
            }

            if (type == typeof(DateTime))
            {
                return(DateTimeParser.ParseDateTime(value));
            }

            Type itemType = TypeExtensions.GetCollectionItemType(type);

            if (itemType != null)
            {
                IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(itemType));

                string[] splits = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string split in splits)
                {
                    list.Add(ParseValue(split.Trim(), itemType));
                }

                return(list);
            }

            // TODO: Figure out how to configure which format provider to use!
            return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the inline type that handles the specified type.
        /// </summary>
        /// <param name="type">The value type.</param>
        /// <returns>The inline type.</returns>
        public static InlineType GetInlineTypeFor(Type type)
        {
            if (type.IsByRef)
            {
                type = type.GetElementType();
            }

            if (TypeExtensions.IsNullable(type))
            {
                type = Nullable.GetUnderlyingType(type);
            }

            return(inlineTypes.Find(delegate(InlineType it) { return it.HandlesType(type); }));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the pattern for the specified type.
        /// </summary>
        /// <param name="type">The value type.</param>
        /// <returns>The pattern.</returns>
        public override string GetPattern(Type type)
        {
            List <string> subPatterns = new List <string>();

            if (TypeExtensions.IsNullable(type))
            {
                type = Nullable.GetUnderlyingType(type);
                subPatterns.Add("(?:null)");
            }

            foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                string parsed = NameParser.Parse(fieldInfo.Name);
                subPatterns.Add("(?:" + string.Join(@"\s*", parsed.Split()) + ")");
            }

            return("(?<{0}>" + string.Join("|", subPatterns.ToArray()) + ")");
        }