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);
        }
示例#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(base.GetPropertyOwner(pd), pd);
                        propertyDescriptorList.Add(cpd);
                    }
                }
            }

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

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

            return(pdcReturn);
        }
        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;
                    }
                }
            }
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            //WriteContext("ConvertFrom", context, value, Type.Missing.GetType());

            ICollection <StandardValueAttribute> col = null;
            Type propType = Type.Missing.GetType();

            if (context != null && context.PropertyDescriptor is CustomPropertyDescriptor)
            {
                CustomPropertyDescriptor cpd = context.PropertyDescriptor as CustomPropertyDescriptor;
                UpdateEnumDisplayText(cpd);
                col      = cpd.StandardValues;
                propType = cpd.PropertyType;
            }
            if (value == null)
            {
                return(null);
            }
            else if (value is string)
            {
                if (propType.IsEnum)
                {
                    var      sInpuValue  = value as string;
                    string[] arrDispName = sInpuValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    var sb = new StringBuilder(1000);
                    foreach (string sDispName in arrDispName)
                    {
                        string sTrimValue = sDispName.Trim();
                        foreach (StandardValueAttribute sva in col)
                        {
                            if (String.Compare(sva.Value.ToString(), sTrimValue, true) == 0 ||
                                String.Compare(sva.DisplayName, sTrimValue, true) == 0)
                            {
                                if (sb.Length > 0)
                                {
                                    sb.Append(",");
                                }
                                sb.Append(sva.Value.ToString());
                            }
                        }
                    }  // end of foreach..loop
                    return(Enum.Parse(propType, sb.ToString(), true));
                }
                foreach (StandardValueAttribute sva in col)
                {
                    if (String.Compare(value.ToString(), sva.DisplayName, true, culture) == 0 ||
                        String.Compare(value.ToString(), sva.Value.ToString(), true, culture) == 0)
                    {
                        return(sva.Value);
                    }
                }
                var tc = TypeDescriptor.GetConverter(propType);
                if (tc != null)
                {
                    object convertedValue = null;
                    try
                    {
                        convertedValue = tc.ConvertFrom(context, culture, value);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    if (tc.IsValid(convertedValue))
                    {
                        return(convertedValue);
                    }
                }
            }
            else if (value.GetType() == propType)
            {
                return(value);
            }
            else if (value is StandardValueAttribute)
            {
                return((value as StandardValueAttribute).Value);
            }

            return(base.ConvertFrom(context, culture, value));
        }