コード例 #1
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection props = _propCache;

            if (props != null)
            {
                return(props);
            }

            // Create the property collection and filter if necessary
            props = new PropertyDescriptorCollection(null);
            //foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(_target, attributes, true))
            //{
            //    props.Add(prop);
            //}
            foreach (FieldInfo field in _target.GetType().GetFields())
            {
                Attribute[]        attrs = (Attribute[])field.GetCustomAttributes(typeof(Attribute), true);
                int                n     = attrs.Length;
                PropertyDescriptor fieldDesc;
                if (field.FieldType.IsPrimitive || field.FieldType.IsEnum || field.FieldType == typeof(Guid) ||
                    field.FieldType == typeof(System.Drawing.Size))
                {
                    Array.Resize <Attribute>(ref attrs, n + 1);
                    attrs[n] = new ReadOnlyAttribute(true);
                    object fld_value = field.GetValue(_target);
                    string str       = fld_value.ToString();
                    if ((field.FieldType != typeof(Guid)) && (fld_value is IFormattable) && (str.Length > 1))
                    {
                        StringBuilder sb  = new StringBuilder();
                        IFormattable  frm = (IFormattable)fld_value;
                        string        hex = frm.ToString("X", Thread.CurrentThread.CurrentCulture);
                        if (field.Name == "Compression")
                        {
                            UInt32 ival   = UInt32.Parse(str);
                            char[] fourcc = new char[4];
                            fourcc[0] = (char)(ival & 0xFF);
                            fourcc[1] = (char)((ival >> 8) & 0xFF);
                            fourcc[2] = (char)((ival >> 16) & 0xFF);
                            fourcc[3] = (char)((ival >> 24) & 0xFF);
                            sb.Append("'");
                            sb.Append(fourcc);
                            sb.Append("' " + str + " (0x" + hex + ")");
                        }
                        else
                        {
                            sb.Append(str + " (0x" + hex + ")");
                        }
                        str = sb.ToString();
                    }
                    fieldDesc = new CustomFieldPropertyDescriptor(str, attrs, field.Name, _target.GetType());
                    //fieldDesc = new FieldPropertyDescriptor(field, attrs);
                }
                else
                {
                    Array.Resize <Attribute>(ref attrs, n + 2);
                    attrs[n]     = new ReadOnlyAttribute(true);
                    attrs[n + 1] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));
                    object fld_value = new FieldsToPropertiesProxyTypeDescriptor(field.GetValue(_target));
                    fieldDesc = new CustomFieldPropertyDescriptor(fld_value, attrs, field.Name, _target.GetType());
                }
                props.Add(fieldDesc);
            }
            _propCache = props;
            return(props);
        }
コード例 #2
0
        //public FieldInfo Field { get { return _field; } }

        public override bool Equals(object obj)
        {
            CustomFieldPropertyDescriptor other = obj as CustomFieldPropertyDescriptor;

            return(other != null && other._field.Equals(_field));
        }