コード例 #1
0
        /// <summary>
        /// String to Int16(字符串 转换成 有符号、数值、整数)
        /// </summary>
        /// <remarks>
        ///  2014-06-23 16:31 Created By iceStone
        /// </remarks>
        /// <param name="s">String</param>
        /// <param name="def">Default</param>
        /// <returns>Byte</returns>
        public static short ToInt16(this string s, short def = default(short))
        {
            short result;

            return(Int16.TryParse(s, out result) ? result : def);
        }
コード例 #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
		}
コード例 #3
0
        static bool Parse <TEnum> (Type enumType, string value, bool ignoreCase, out TEnum result)
        {
            result = default(TEnum);

            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)
            {
                result = (TEnum)info.values.GetValue(loc);
                return(true);
            }

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

            // is 'value' a list of named constants?
            if (value.IndexOf(',') != -1)
            {
                if (split_char == null)
                {
                    split_char = new [] { ',' }
                }
                ;
                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)
                    {
                        return(false);
                    }

                    retVal |= GetValue(info.values.GetValue(loc), typeCode);
                }
                result = (TEnum)ToObject(enumType, retVal);
                return(true);
            }

            // is 'value' a number?
            switch (typeCode)
            {
            case TypeCode.SByte:
                sbyte sb;
                if (!SByte.TryParse(value, out sb))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, sb);
                break;

            case TypeCode.Byte:
                byte b;
                if (!Byte.TryParse(value, out b))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, b);
                break;

            case TypeCode.Int16:
                short i16;
                if (!Int16.TryParse(value, out i16))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, i16);
                break;

            case TypeCode.UInt16:
                ushort u16;
                if (!UInt16.TryParse(value, out u16))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, u16);
                break;

            case TypeCode.Int32:
                int i32;
                if (!Int32.TryParse(value, out i32))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, i32);
                break;

            case TypeCode.UInt32:
                uint u32;
                if (!UInt32.TryParse(value, out u32))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, u32);
                break;

            case TypeCode.Int64:
                long i64;
                if (!Int64.TryParse(value, out i64))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, i64);
                break;

            case TypeCode.UInt64:
                ulong u64;
                if (!UInt64.TryParse(value, out u64))
                {
                    return(false);
                }
                result = (TEnum)ToObject(enumType, u64);
                break;

            default:
                break;
            }

            return(true);
        }