示例#1
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("not an Enum type", "enumType");
            }

            EnumInfo info = enumType.EnumInfo;

            if (info == null)
            {
                throw new ArgumentException("EnumInfo is null");
            }

            value = value.Trim();
            if (value.Length == 0)
            {
                throw new ArgumentException("cannot be an empty string", "value");
            }

            // is 'value' a named constant?
            int loc = FindName(info.names, value, ignoreCase);

            if (loc >= 0)
            {
                return(info.values[loc]);
            }

            TypeCode typeCode = GetVTypeCode(enumType);

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

            // is 'value' a number?
            try
            {
                return(ToObject(enumType, ParseValue(value, typeCode)));
            }
            catch (Exception e)
            {
                throw new ArgumentException(String.Format("The requested value `{0}' was not found", value), e);
            }
        }
示例#2
0
        static string FormatFlags(Type enumType, object value)
        {
            EnumInfo info = enumType.EnumInfo;

            if (info == null)
            {
                throw new ArgumentException("enumInfo is null");
            }

            string asString = value.ToString();

            if (asString == "0")
            {
                string name = GetName(enumType, value);
                if (name == null)
                {
                    return(asString);
                }
                return(name);
            }

            string retVal = "";
            int    n      = info.values.Length;

            TypeCode typeCode = Type.GetTypeCode(enumType.UnderlyingSystemType);

            // This is ugly, yes.  We need to handle the different integer
            // types for enums.  If someone else has a better idea, be my guest.
            switch (typeCode)
            {
            case TypeCode.SByte:
            {
                sbyte flags = (sbyte)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    sbyte enumValue = (sbyte)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.Byte:
            {
                byte flags = (byte)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    byte enumValue = (byte)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.Int16:
            {
                short flags = (short)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    short enumValue = (short)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.Int32:
            {
                int flags = (int)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    int enumValue = (int)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.UInt16:
            {
                ushort flags = (ushort)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    ushort enumValue = (ushort)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.UInt32:
            {
                uint flags = (uint)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    uint enumValue = (uint)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.Int64:
            {
                long flags = (long)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    long enumValue = (long)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;

            case TypeCode.UInt64:
            {
                ulong flags = (ulong)value;
                for (int i = n - 1; i >= 0; i--)
                {
                    ulong enumValue = (ulong)info.values[i];
                    if (enumValue == 0)
                    {
                        continue;
                    }

                    if ((flags & enumValue) == enumValue)
                    {
                        if (retVal.Length > 0)
                        {
                            retVal = info.names[i] + ", " + retVal;
                        }
                        else
                        {
                            retVal = info.names[i];
                        }
                        flags -= enumValue;
                    }
                }
                if (flags != 0)
                {
                    return(asString);
                }
            }
            break;
            }

            if (retVal == "")
            {
                return(asString);
            }

            return(retVal);
        }
示例#3
0
        internal static string Format(Type enumType, object value, string format)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }

            if (!enumType.IsEnum)
            {
                throw new ArgumentException("Type provided must be an Enum.");
            }

            Type vType          = value.GetType();
            Type underlyingType = GetUnderlyingType(enumType);

            if (vType.IsEnum)
            {
                if (vType != enumType)
                {
                    throw new ArgumentException(string.Format("Object must be the same type as the enum. The type" +
                                                              " passed in was {0}; the enum type was {1}.",
                                                              vType.FullName, enumType.FullName));
                }
            }
            else if (vType != underlyingType)
            {
                throw new ArgumentException(string.Format("Enum underlying type and the object must be the same type" +
                                                          " or object. Type passed in was {0}; the enum underlying" +
                                                          " type was {1}.", vType.FullName, underlyingType.FullName));
            }

            if (format.Length != 1)
            {
                throw new FormatException("Format String can be only \"G\",\"g\",\"X\"," +
                                          "\"x\",\"F\",\"f\",\"D\" or \"d\".");
            }

            Enum e = value as Enum;

            if (e != null)
            {
                value = e.GetValue();
            }

            char   formatChar = format[0];
            string retVal;

            if ((formatChar == 'G' || formatChar == 'g'))
            {
                EnumInfo info = enumType.EnumInfo;
                if (info == null)
                {
                    throw new ArgumentException("enumInfo is null");
                }
                if (!info.flags)
                {
                    retVal = GetName(enumType, value);
                    if (retVal == null)
                    {
                        retVal = value.ToString();
                    }

                    return(retVal);
                }

                formatChar = 'f';
            }

            if ((formatChar == 'f' || formatChar == 'F'))
            {
                return(FormatFlags(enumType, value));
            }

            switch (formatChar)
            {
            case 'X':
                retVal = FormatHex(enumType, value, true);
                break;

            case 'x':
                retVal = FormatHex(enumType, value, false);
                break;

            case 'D':
            case 'd':
                if (underlyingType == typeof(ulong))
                {
                    ulong ulongValue = Convert.ToUInt64(value);
                    retVal = ulongValue.ToString();
                }
                else
                {
                    long longValue = Convert.ToInt64(value);
                    retVal = longValue.ToString();
                }
                break;

            default:
                throw new FormatException("Format String can be only \"G\",\"g\",\"X\"," +
                                          "\"x\",\"F\",\"f\",\"D\" or \"d\".");
            }
            return(retVal);
        }