Example #1
0
        // Parses an integer from a String in the given style. Returns false rather
        // than throwing except in if input is invalid
        //
        public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out TName result)
        {
            if (s == null)
            {
                result = 0;
                return(false);
            }

            var r = TType.TryParse(s, style, NumberFormatInfo.GetInstance(provider), out var res);

            result = res;
            return(r);
        }
Example #2
0
		public static object Parse (Type enumType, string value, bool ignoreCase)
		{
			if (enumType == null)
				throw new ArgumentNullException ("enumType");

			if (value == null)
				throw new ArgumentNullException ("value");

			if (!enumType.IsEnum)
				throw new ArgumentException ("enumType is not an Enum type.", "enumType");

			value = value.Trim ();
			if (value.Length == 0)
				throw new ArgumentException ("An empty string is not considered a valid value.");

			MonoEnumInfo info;
			MonoEnumInfo.GetInfo (enumType, out info);

			// is 'value' a named constant?
			int loc = FindName (info.name_hash, info.names, value, ignoreCase);
			if (loc >= 0)
				return info.values.GetValue (loc);

			TypeCode typeCode = ((Enum) info.values.GetValue (0)).GetTypeCode ();

			// is 'value' a list of named constants?
			if (value.IndexOf (',') != -1) {
				string [] names = value.Split (split_char);
				ulong retVal = 0;
				for (int i = 0; i < names.Length; ++i) {
					loc = FindName (info.name_hash, info.names, names [i].Trim (), ignoreCase);
					if (loc < 0)
						throw new ArgumentException ("The requested value was not found.");
					retVal |= GetValue (info.values.GetValue (loc), typeCode);
				}
				return ToObject (enumType, retVal);
			}

			// is 'value' a number?
#if !NET_2_0
			try {
				return ToObject (enumType, Convert.ChangeType (value, typeCode));
			} catch (FormatException) {
				throw new ArgumentException (String.Format ("The requested value '{0}' was not found.", value));
			}
#else
			switch (typeCode) {
			case TypeCode.SByte:
				sbyte sb;
				if (SByte.TryParse (value, out sb))
					return ToObject (enumType, sb);
				break;
			case TypeCode.Byte:
				byte b;
				if (Byte.TryParse (value, out b))
					return ToObject (enumType, b);
				break;
			case TypeCode.Int16:
				short i16;
				if (Int16.TryParse (value, out i16))
					return ToObject (enumType, i16);
				break;
			case TypeCode.UInt16:
				ushort u16;
				if (UInt16.TryParse (value, out u16))
					return ToObject (enumType, u16);
				break;
			case TypeCode.Int32:
				int i32;
				if (Int32.TryParse (value, out i32))
					return ToObject (enumType, i32);
				break;
			case TypeCode.UInt32:
				uint u32;
				if (UInt32.TryParse (value, out u32))
					return ToObject (enumType, u32);
				break;
			case TypeCode.Int64:
				long i64;
				if (Int64.TryParse (value, out i64))
					return ToObject (enumType, i64);
				break;
			case TypeCode.UInt64:
				ulong u64;
				if (UInt64.TryParse (value, out u64))
					return ToObject (enumType, u64);
				break;
			default:
				break;
			}
			throw new ArgumentException (String.Format ("The requested value '{0}' was not found.", value));
#endif
		}