public CustomPropertyDescriptor CreateProperty(string name, Type type, object value, int index, params Attribute[] attributes)
        {
            var cpd = new CustomPropertyDescriptor(instance, name, type, value, attributes);

            if (index == -1)
            {
                propertyDescriptorList.Add(cpd);
            }
            else
            {
                propertyDescriptorList.Insert(index, cpd);
            }
            TypeDescriptor.Refresh(instance);
            return(cpd);
        }
Ejemplo n.º 2
0
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                var standardProperties = base.GetProperties(context, value, attributes);
                var obj         = value as CustomObject;
                var customProps = obj == null ? null : obj.Properties;
                var props       = new PropertyDescriptor[standardProperties.Count + (customProps == null ? 0 : customProps.Count)];

                standardProperties.CopyTo(props, 0);
                if (customProps != null)
                {
                    var index = standardProperties.Count;
                    foreach (var property in customProps)
                    {
                        props[index++] = new CustomPropertyDescriptor(property);
                    }
                }
                return(new PropertyDescriptorCollection(props));
            }
        public override sealed PropertyDescriptorCollection GetProperties()
        {
            if (propertyDescriptorList.Count == 0)
            {
                var pdc = base.GetProperties();  // this gives us a readonly collection, no good
                foreach (PropertyDescriptor pd in pdc)
                {
                    if (!(pd is CustomPropertyDescriptor))
                    {
                        var cpd = new CustomPropertyDescriptor(GetPropertyOwner(pd), pd);
                        propertyDescriptorList.Add(cpd);
                    }
                }
            }

            var pdl = propertyDescriptorList.FindAll(pd => pd != null);

            PreProcess(pdl);
            var pdcReturn = new PropertyDescriptorCollection(propertyDescriptorList.ToArray());

            return(pdcReturn);
        }
Ejemplo n.º 4
0
        private void UpdateEnumDisplayText(CustomPropertyDescriptor cpd)
        {
            if (!(cpd.PropertyType.IsEnum || cpd.PropertyType == typeof(bool)))
            {
                return;
            }
            if ((cpd.PropertyFlags & PropertyFlags.LocalizeEnumerations) <= 0)
            {
                return;
            }
            var                    prefix = String.Empty;
            ResourceManager        rm     = null;
            StandardValueAttribute sva;

            sva = cpd.StandardValues.FirstOrDefault();

            // first try property itself
            if (cpd.ResourceManager != null)
            {
                var keyName   = cpd.KeyPrefix + cpd.Name + "_" + sva?.Value + "_Name";
                var 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)
            {
                var keyName   = cpd.KeyPrefix + cpd.PropertyType.Name + "_" + sva?.Value + "_Name";
                var 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 (var standardValueAttribute in cpd.StandardValues)
                {
                    var keyName     = prefix + "_" + standardValueAttribute.Value + "_Name"; // display name
                    var keyDesc     = prefix + "_" + standardValueAttribute.Value + "_Desc"; // description
                    var dispName    = String.Empty;
                    var description = String.Empty;

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

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