예제 #1
0
        internal static IEnumerable <StandardValueAttribute> GetEnumItems(Type enumType)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException(EnumInstanceIsNull);
            }

            if (!enumType.IsEnum)
            {
                throw new ArgumentException(EnumInstanceIsNotEnumType);
            }

            var attributeArrayList = new ArrayList();
            var fields             = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);

            foreach (var fi in fields)
            {
                var attr = fi.GetCustomAttributes(typeof(StandardValueAttribute), false) as StandardValueAttribute[];

                if (attr != null && attr.Length > 0)
                {
                    attr[0].value = fi.GetValue(null);
                    attributeArrayList.Add(attr[0]);
                }
                else
                {
                    var atr = new StandardValueAttribute(fi.GetValue(null));
                    attributeArrayList.Add(atr);
                }
            }
            var retAttr = attributeArrayList.ToArray(typeof(StandardValueAttribute)) as StandardValueAttribute[];

            return(retAttr);
        }
        private void UpdateMemberData()
        {
            if (propertyDescriptor != null)
            {
                value = propertyDescriptor.GetValue(owner);
            }

            if (PropertyType.IsEnum)
            {
                var sva = StandardValueAttribute.GetEnumItems(PropertyType);
                standardValues.AddRange(sva);
            }
            else if (PropertyType == typeof(bool))
            {
                standardValues.Add(new StandardValueAttribute(true));
                standardValues.Add(new StandardValueAttribute(false));
            }
        }
        private void UpdateEnumDisplayText(CustomPropertyDescriptor cpd)
        {
            if (!(cpd.PropertyType.IsEnum || cpd.PropertyType == typeof(bool)))
            {
                return;
            }
            if ((cpd.PropertyFlags & PropertyFlags.LocalizeEnumerations) <= 0)
            {
                return;
            }
            string                 prefix = String.Empty;
            ResourceManager        rm     = null;
            StandardValueAttribute sva    = null;

            sva = cpd.StandardValues.FirstOrDefault() as StandardValueAttribute;

            // first try property itself
            if (cpd.ResourceManager != null)
            {
                string keyName   = cpd.KeyPrefix + cpd.Name + "_" + sva.Value.ToString() + "_Name";
                string valueName = cpd.ResourceManager.GetString(keyName);
                if (!String.IsNullOrWhiteSpace(valueName))
                {
                    rm     = cpd.ResourceManager;
                    prefix = cpd.KeyPrefix + cpd.Name;
                }
            }

            // now try class level
            if (rm == null && cpd.ResourceManager != null)
            {
                string keyName   = cpd.KeyPrefix + cpd.PropertyType.Name + "_" + sva.Value.ToString() + "_Name";
                string valueName = cpd.ResourceManager.GetString(keyName);
                if (!String.IsNullOrWhiteSpace(valueName))
                {
                    rm     = cpd.ResourceManager;
                    prefix = cpd.KeyPrefix + cpd.PropertyType.Name;
                }
            }

            // try the enum itself if still null
            if (rm == null && cpd.PropertyType.IsEnum)
            {
                var attr = (EnumResourceAttribute)cpd.AllAttributes.FirstOrDefault(a => a is EnumResourceAttribute);
                if (attr != null)
                {
                    try
                    {
                        if (String.IsNullOrWhiteSpace(attr.AssemblyFullName) == false)
                        {
                            rm = new ResourceManager(attr.BaseName, Assembly.ReflectionOnlyLoad(attr.AssemblyFullName));
                        }
                        else
                        {
                            rm = new ResourceManager(attr.BaseName, cpd.PropertyType.Assembly);
                        }
                        prefix = attr.KeyPrefix + cpd.PropertyType.Name;
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }

            if (rm != null)
            {
                foreach (StandardValueAttribute sv in cpd.StandardValues)
                {
                    string keyName     = prefix + "_" + sv.Value.ToString() + "_Name"; // display name
                    string keyDesc     = prefix + "_" + sv.Value.ToString() + "_Desc"; // description
                    string dispName    = String.Empty;
                    string description = String.Empty;

                    try
                    {
                        dispName = rm.GetString(keyName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (String.IsNullOrWhiteSpace(dispName) == false)
                    {
                        sv.DisplayName = dispName;
                    }

                    try
                    {
                        description = rm.GetString(keyDesc);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (String.IsNullOrWhiteSpace(description) == false)
                    {
                        sv.Description = description;
                    }
                }
            }
        }