Exemplo n.º 1
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            bool filtering = (null != attributes && 0 < attributes.Length);

            PropertyDescriptorCollection props = InitializePropertyDescriptors();

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

            FilterCache cache = _filterCache;

            // Use a cached version if possible
            if (filtering && cache != null && cache.IsValid(attributes))
            {
                return(cache.FilteredProperties);
            }
            else if (!filtering && props != null)
            {
                return(props);
            }

            //Build up the attribute cache, since our PropertyDescriptor doesn't store it internally.
            // _values is set only during construction.
            if (null == _attrCache && null != attributes && 0 < attributes.Length)
            {
                _attrCache = new Dictionary <object, AttributeCollection>();
                foreach (FieldDescriptor pd in _propertyDescriptors)
                {
                    object      o         = pd.GetValue(this);
                    object[]    atts      = o.GetType().GetCustomAttributes(/*inherit*/ false); //atts will not be null (atts.Length==0)
                    Attribute[] attrArray = new Attribute[atts.Length];
                    atts.CopyTo(attrArray, 0);
                    _attrCache.Add(pd, new AttributeCollection(attrArray));
                }
            }

            //Create the filter based on the attributes.
            props = new PropertyDescriptorCollection(null);
            foreach (PropertyDescriptor pd in _propertyDescriptors)
            {
                if (_attrCache[pd].Matches(attributes))
                {
                    props.Add(pd);
                }
            }

            // Store the computed properties
            if (filtering)
            {
                cache                    = new FilterCache();
                cache.Attributes         = attributes;
                cache.FilteredProperties = props;
                _filterCache             = cache;
            }

            return(props);
        }
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            bool filtering = (attributes != null && attributes.Length > 0);
            PropertyDescriptorCollection props = _propCache;
            FilterCache cache = _filterCache;

            // Use a cached version if we can
            if (filtering && cache != null && cache.IsValid(attributes))
            {
                return(cache.FilteredProperties);
            }
            if (!filtering && props != null)
            {
                return(props);
            }

            // загрузка доступных ресурсов
            var manifests = GetType().Assembly.GetManifestResourceNames();
            var manager   = manifests.Select(m => m.Replace(".resources", string.Empty))
                            .Select(manifest => new ResourceManager(manifest, GetType().Assembly))
                            .ToList();

            // Create the property collection and filter if necessary
            props = new PropertyDescriptorCollection(null);
            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this, attributes, true))
            {
                var attr = prop.Attributes.Cast <Attribute>().ToArray();

                // проверка атрибута локализации
                var lo = prop.Attributes[typeof(LocalizableAttribute)];
                if (lo != null && ((LocalizableAttribute)lo).IsLocalizable)
                {
                    // создание локализуемой версии свойства
                    props.Add(new LocalizePropertDescription(prop, attr, manager));
                }
                else
                {
                    // копирование свойства без изменений
                    props.Add(prop);
                }
            }

            // Store the computed properties
            if (filtering)
            {
                cache = new FilterCache
                {
                    Attributes         = attributes,
                    FilteredProperties = props
                };
                _filterCache = cache;
            }
            else
            {
                _propCache = props;
            }

            return(props);
        }
Exemplo n.º 3
0
            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                // Retrieve cached properties and filtered properties
                bool        filtering = attributes is { Length : > 0 };
                FilterCache cache     = _provider._filterCache;
                PropertyDescriptorCollection properties = _provider._propCache;

                // Use a cached version if we can
                if (filtering && cache != null && cache.IsValid(attributes))
                {
                    return(cache.FilteredProperties);
                }
                if (!filtering && properties != null)
                {
                    return(properties);
                }

                // Otherwise, create the property collection
                properties = new PropertyDescriptorCollection(null);

                foreach (PropertyInfo property in _objectType.GetProperties())
                {
                    // FieldInfo[] pflds = p.PropertyType.GetFields();
                    PropertyInfo[] propertyInfos = property.PropertyType.GetProperties();
                    // if the property in not an array and has public fields or properties - use ExpandablePropertyDescriptor
                    PropertyDescriptor desc = !property.PropertyType.HasElementType && propertyInfos.Length > 0
                                                ? new ExpandablePropertyDescriptor(property)
                                                : new DefaultPropertyDescriptor(property);

                    if (!filtering || desc.Attributes.Contains(attributes))
                    {
                        properties.Add(desc);
                    }
                }

                // Store the updated properties
                if (filtering)
                {
                    cache = new FilterCache
                    {
                        FilteredProperties = properties,
                        Attributes         = attributes
                    };
                    _provider._filterCache = cache;
                }
                else
                {
                    _provider._propCache = properties;
                }

                // Return the computed properties
                return(properties);
            }
        }
Exemplo n.º 4
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            bool filtering = (attributes != null && attributes.Length > 0);
            PropertyDescriptorCollection props = cachedPropertyDescriptors;
            FilterCache cache = cachedFilter;

            // Use a cached version if possible
            if (filtering && cache != null && cache.IsValid(attributes))
            {
                return(cache.FilteredProperties);
            }
            else if (!filtering && props != null)
            {
                return(props);
            }

            // Create the property collection and filter
            props = new PropertyDescriptorCollection(null);
            foreach (PropertyDescriptor prop in
                     TypeDescriptor.GetProperties(
                         this, attributes, true))
            {
                props.Add(prop);
            }
            foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                FieldPropertyDescriptor fieldDesc = new FieldPropertyDescriptor(field);
                if (!filtering || fieldDesc.Attributes.Contains(attributes))
                {
                    props.Add(fieldDesc);
                }
            }

            // Store the computed properties
            if (filtering)
            {
                cache                    = new FilterCache();
                cache.Attributes         = attributes;
                cache.FilteredProperties = props;
                cachedFilter             = cache;
            }
            else
            {
                cachedPropertyDescriptors = props;
            }

            return(props);
        }
Exemplo n.º 5
0
        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            // Retrieve cached properties and filtered properties
            bool        filtering = attributes != null && attributes.Length > 0;
            FilterCache cache     = _provider._filterCache;
            PropertyDescriptorCollection props = _provider._propCache;

            // Use a cached version if we can
            if (filtering && cache != null && cache.IsValid(attributes))
            {
                return(cache.FilteredProperties);
            }
            else if (!filtering && props != null)
            {
                return(props);
            }

            // Otherwise, create the property collection
            props = new PropertyDescriptorCollection(null);
            foreach (PropertyDescriptor prop in base.GetProperties(attributes))
            {
                props.Add(prop);
            }
            foreach (FieldInfo field in _objectType.GetFields())
            {
                FieldPropertyDescriptor fieldDesc = new FieldPropertyDescriptor(field);
                if (!filtering || fieldDesc.Attributes.Contains(attributes))
                {
                    props.Add(fieldDesc);
                }
            }

            // Store the updated properties
            if (filtering)
            {
                cache = new FilterCache();
                cache.FilteredProperties = props;
                cache.Attributes         = attributes;
                _provider._filterCache   = cache;
            }
            else
            {
                _provider._propCache = props;
            }

            // Return the computed properties
            return(props);
        }
Exemplo n.º 6
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            bool filtering = (attributes != null && attributes.Length > 0);
            PropertyDescriptorCollection props = _propCache;
            FilterCache cache = _filterCache;

            // Use a cached version if we can
            if (filtering && cache != null && cache.IsValid(attributes))
            {
                return(cache.FilteredProperties);
            }
            else if (!filtering && 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))
            {
                string displayNameLng = LanguageHelper.Instance.ReadKeyValueFromResources(formName, prop.Name);
                string categoryLng    = LanguageHelper.Instance.ReadKeyValueFromResources(formName, prop.Category);

                props.Add(new LngPropertyDescriptor(prop, displayNameLng, "", categoryLng));
            }

            // Store the computed properties
            if (filtering)
            {
                cache                    = new FilterCache();
                cache.Attributes         = attributes;
                cache.FilteredProperties = props;
                _filterCache             = cache;
            }
            else
            {
                _propCache = props;
            }

            return(props);
        }