Example #1
0
        public static object EnumToObject(PropertyGridOptionsAttribute options, Type propertyType, object value)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (propertyType == null)
            {
                throw new ArgumentNullException("propertyType");
            }

            if (value != null && propertyType.IsEnum)
            {
                return(Extensions.EnumToObject(propertyType, value));
            }

            if (value != null && value.GetType().IsEnum)
            {
                return(Extensions.EnumToObject(value.GetType(), value));
            }

            if (propertyType != typeof(string))
            {
                return(ConversionService.ChangeType(value, propertyType));
            }

            if (options == null || options.EnumNames == null || options.EnumValues == null || options.EnumValues.Length != options.EnumNames.Length)
            {
                return(ConversionService.ChangeType(value, propertyType));
            }

            if (BaseConverter.IsNullOrEmptyString(value))
            {
                return(string.Empty);
            }

            StringBuilder sb     = new StringBuilder();
            string        svalue = string.Format("{0}", value);
            ulong         ul;

            if (!ulong.TryParse(svalue, out ul))
            {
                var enums = ParseEnum(svalue);
                if (enums.Count == 0)
                {
                    return(string.Empty);
                }

                var enumValues = options.EnumValues.Select(v => string.Format("{0}", v)).ToArray();
                foreach (string enumValue in enums)
                {
                    int index = IndexOf(enumValues, enumValue);
                    if (index < 0)
                    {
                        index = IndexOf(options.EnumNames, enumValue);
                    }
                    if (index >= 0)
                    {
                        if (sb.Length > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(options.EnumNames[index]);
                    }
                }
            }
            else // a string
            {
                ulong bitsCount = (ulong)GetEnumMaxPower(options) - 1; // skip first
                ulong b         = 1;
                for (ulong bit = 1; bit < bitsCount; bit++) // signed, skip highest bit
                {
                    if ((ul & b) != 0)
                    {
                        int index = IndexOf(options.EnumValues, b);
                        if (index >= 0)
                        {
                            if (sb.Length > 0)
                            {
                                sb.Append(", ");
                            }
                            sb.Append(options.EnumNames[index]);
                        }
                    }
                    b *= 2;
                }
            }

            return(sb.ToString());
        }