private static IDictionary <Type, TypeCode> InitTypeToTypeCodeLookupTable()
        {
            Dictionary <Type, TypeCode> result = new Dictionary <Type, TypeCode>();

            foreach (var pair in EnumUtility.ToEnumerable <TypeCode>())
            {
                try
                {
                    Type validType = Type.GetType(string.Format("System.{0}", pair.Value), false);
                    if (validType != null)
                    {
                        result.Add(validType, EnumUtility.Parse <TypeCode>(pair.Value));
                    }
                }
                catch (Exception)
                {
                }
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Converts the string representation of a GUID to its equivalent <see cref="Guid"/> structure.
        /// </summary>
        /// <param name="value">The GUID to convert.</param>
        /// <param name="format">A bitmask comprised of one or more <see cref="GuidFormats"/> that specify how the GUID parsing is conducted.</param>
        /// <param name="result">The structure that will contain the parsed value.</param>
        /// <returns><c>true</c> if the parse operation was successful; otherwise, <c>false</c>.</returns>
        /// <remarks>
        /// This method returns <c>false</c> if <paramref name="value"/> is <c>null</c> or not in a recognized format, and does not throw an exception.<br/>
        /// Only 32 digits (<see cref="GuidFormats.NumberFormat"/>); 32 digits separated by hyphens (<see cref="GuidFormats.DigitFormat"/>); 32 digits separated by hyphens, enclosed in brackets (<see cref="GuidFormats.BraceFormat"/>) and 32 digits separated by hyphens, enclosed in parentheses (<see cref="GuidFormats.ParenthesisFormat"/>) is supported.<br/>
        /// For more information refer to this page @ StackOverflow: http://stackoverflow.com/questions/968175/what-is-the-string-length-of-a-guid
        /// </remarks>
        public static bool TryParse(string value, GuidFormats format, out Guid result)
        {
            result = Guid.Empty;
            if (string.IsNullOrEmpty(value))
            {
                return(true);
            }
            if (value.Length < 32)
            {
                return(false);
            }
            if (value.Length > 38)
            {
                return(false);
            }

            int  startPosition  = 0;
            bool hasDashes      = (value.IndexOf('-') > 0);
            bool hasBraces      = (value.StartsWith("{", StringComparison.OrdinalIgnoreCase) && value.EndsWith("}", StringComparison.OrdinalIgnoreCase));
            bool hasParenthesis = (value.StartsWith("(", StringComparison.OrdinalIgnoreCase) && value.EndsWith(")", StringComparison.OrdinalIgnoreCase));


            if (hasBraces || hasParenthesis) // BraceFormat or ParenthesisFormat
            {
                if (!EnumUtility.HasFlag(format, GuidFormats.BraceFormat) && !EnumUtility.HasFlag(format, GuidFormats.ParenthesisFormat))
                {
                    return(false);
                }
                if (value.Length != 38)
                {
                    return(false);
                }
                startPosition = 1;
            }

            if (hasDashes) // DigitFormat
            {
                if (!EnumUtility.HasFlag(format, GuidFormats.DigitFormat) && startPosition == 0)
                {
                    return(false);
                }
                if ((!EnumUtility.HasFlag(format, GuidFormats.BraceFormat) && startPosition == 1) && (!EnumUtility.HasFlag(format, GuidFormats.ParenthesisFormat) && startPosition == 1))
                {
                    return(false);
                }
                if (value.Length != 36 && startPosition == 0)
                {
                    return(false);
                }

                if (value[startPosition + 8] != '-' ||
                    value[startPosition + 13] != '-' ||
                    value[startPosition + 18] != '-' ||
                    value[startPosition + 23] != '-')
                {
                    return(false);
                }
            }
            else // NumberFormat
            {
                if (!EnumUtility.HasFlag(format, GuidFormats.NumberFormat))
                {
                    return(false);
                }
                if (value.Length != 32)
                {
                    return(false);
                }
            }


            if (!IsCharWiseValid(value, hasDashes, hasParenthesis, hasBraces))
            {
                return(false);
            }

            try
            {
                result = new Guid(value);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #3
0
 /// <summary>
 /// Converts the string representation of the name or numeric <paramref name="value"/> of one or more enumerated constants to an equivalent enumerated <typeparamref name="TEnum"/>.
 /// </summary>
 /// <typeparam name="TEnum">The type of the enumeration to convert.</typeparam>
 /// <param name="value">A string containing the name or value to convert.</param>
 /// <param name="ignoreCase"><c>true</c> to ignore case; <c>false</c> to regard case.</param>
 /// <returns>An enum of type <typeparamref name="TEnum" /> whose value is represented by <paramref name="value" />.</returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="value"/> is null.
 /// </exception>
 /// <exception cref="TypeArgumentException">
 /// <typeparamref name="TEnum"/> does not represents an enumeration.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// <paramref name="value"/> does not represents an enumeration.
 /// </exception>
 public static TEnum ToEnum <TEnum>(this string value, bool ignoreCase) where TEnum : struct, IConvertible
 {
     return(EnumUtility.Parse <TEnum>(value, ignoreCase));
 }