Пример #1
0
        public static SByte?ToSByteNullable(this String Expr)
        {
            SByte?result = null;

            if (Expr.IsNullOrEmpty())
            {
                return(result);
            }

            Expr = Expr.Trim();

            SByte newSByte;

            if (SByte.TryParse(Expr, out newSByte))
            {
                result = newSByte;
            }

            return(result);
        }
Пример #2
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)
            {
                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);
        }
Пример #3
0
        private static bool TryParseAsInteger(EETypePtr enumEEType, String value, out Object result)
        {
            result = null;

            if (value.Length == 0)
            {
                return(false);
            }

            if (!(Char.IsDigit(value[0]) || value[0] == '+' || value[0] == '-'))
            {
                return(false);
            }
            RuntimeImports.RhCorElementType corElementType = enumEEType.CorElementType;

            unsafe
            {
                switch (corElementType)
                {
                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN:
                {
                    Boolean v;
                    if (!Boolean.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR:
                {
                    Char v;
                    if (!Char.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1:
                {
                    SByte v;
                    if (!SByte.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1:
                {
                    Byte v;
                    if (!Byte.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2:
                {
                    Int16 v;
                    if (!Int16.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2:
                {
                    UInt16 v;
                    if (!UInt16.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4:
                {
                    Int32 v;
                    if (!Int32.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4:
                {
                    UInt32 v;
                    if (!UInt32.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8:
                {
                    Int64 v;
                    if (!Int64.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8:
                {
                    UInt64 v;
                    if (!UInt64.TryParse(value, out v))
                    {
                        return(false);
                    }
                    result = RuntimeImports.RhBox(enumEEType, &v);
                    return(true);
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
Пример #4
0
        public static bool TryParseEnum <TEnum>(this string value, out TEnum result) where TEnum : struct
        {
            if (string.IsNullOrEmpty(value))
            {
                result = default(TEnum);
                return(false);
            }
            var enumType = typeof(TEnum);

            if (!enumType.IsEnum)
            {
                throw new ArgumentException(string.Format("Type '{0}' is not an enum", enumType.FullName));
            }
            result = default(TEnum);
            // Try to parse the value directly
            if (Enum.IsDefined(enumType, value))
            {
                result = (TEnum)Enum.Parse(enumType, value);
                return(true);
            }
            // Get some info on enum
            var enumValues = Enum.GetValues(enumType);

            if (enumValues.Length == 0)
            {
                return(false);
            }
            // probably can't happen as you cant define empty enum?
            var enumTypeCode = Type.GetTypeCode(enumValues.GetValue(0).GetType());

            // Try to parse it as a flag
            if (value.IndexOf(',') != -1)
            {
                if (!Attribute.IsDefined(enumType, typeof(FlagsAttribute)))
                {
                    return(false);
                }
                // value has flags but enum is not flags
                // todo: cache this for efficiency
                var enumInfo  = new Dictionary <string, object>();
                var enumNames = Enum.GetNames(enumType);
                for (var i = 0; i < enumNames.Length; i++)
                {
                    enumInfo.Add(enumNames[i], enumValues.GetValue(i));
                }
                ulong retVal = 0;
                foreach (var name in value.Split(FlagDelimiter))
                {
                    var trimmedName = name.Trim();
                    if (!enumInfo.ContainsKey(trimmedName))
                    {
                        return(false);
                    }
                    // Enum has no such flag
                    var   enumValueObject = enumInfo[trimmedName];
                    ulong enumValueLong;
                    switch (enumTypeCode)
                    {
                    case TypeCode.Byte:
                        enumValueLong = (byte)enumValueObject;
                        break;

                    case TypeCode.SByte:
                        enumValueLong = (byte)((sbyte)enumValueObject);
                        break;

                    case TypeCode.Int16:
                        enumValueLong = (ushort)((short)enumValueObject);
                        break;

                    case TypeCode.Int32:
                        enumValueLong = (uint)((int)enumValueObject);
                        break;

                    case TypeCode.Int64:
                        enumValueLong = (ulong)((long)enumValueObject);
                        break;

                    case TypeCode.UInt16:
                        enumValueLong = (ushort)enumValueObject;
                        break;

                    case TypeCode.UInt32:
                        enumValueLong = (uint)enumValueObject;
                        break;

                    case TypeCode.UInt64:
                        enumValueLong = (ulong)enumValueObject;
                        break;

                    default:
                        return(false);
                        // should never happen
                    }
                    retVal |= enumValueLong;
                }
                result = (TEnum)Enum.ToObject(enumType, retVal);
                return(true);
            }
            // the value may be a number, so parse it directly
            switch (enumTypeCode)
            {
            case TypeCode.SByte:
                sbyte sb;
                if (!SByte.TryParse(value, out sb))
                {
                    return(false);
                }
                result = (TEnum)Enum.ToObject(enumType, sb);
                break;

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

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

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

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

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

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

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

            default:
                return(false);
                // should never happen
            }
            return(true);
        }
Пример #5
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
        }
Пример #6
0
        /// <summary>
        /// String to SByte(字符串 转换成 有符号、数值、整数)
        /// </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 sbyte ToSByte(this string s, sbyte def = default(sbyte))
        {
            sbyte result;

            return(SByte.TryParse(s, out result) ? result : def);
        }