Beispiel #1
0
        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            var sourceProperties = base.GetProperties(attributes);

            if ((sourceProperties == null) || (sourceProperties.Count == 0))
            {
                return(sourceProperties);
            }

            var entry             = this.GetEntry();
            var replaceProperties = default(Dictionary <PropertyDescriptor, DataItemPropertyDescriptorBase>);

            lock ( entry )
            {
                replaceProperties = entry.Properties;

                if (replaceProperties == null)
                {
                    replaceProperties = new Dictionary <PropertyDescriptor, DataItemPropertyDescriptorBase>();
                    entry.Properties  = replaceProperties;

                    var factory = new DataItemPropertyDescriptorFactory(this);

                    foreach (var sourceProperty in sourceProperties.Cast <PropertyDescriptor>())
                    {
                        var descriptor = factory.CreatePropertyDescriptor(sourceProperty);
                        if (descriptor != null)
                        {
                            replaceProperties.Add(sourceProperty, descriptor);
                        }
                    }
                }

                if (replaceProperties.Count == 0)
                {
                    return(sourceProperties);
                }
            }

            var destinationProperties = new PropertyDescriptor[sourceProperties.Count];

            for (int i = 0; i < sourceProperties.Count; i++)
            {
                var sourceProperty = sourceProperties[i];

                DataItemPropertyDescriptorBase destinationProperty;
                if (replaceProperties.TryGetValue(sourceProperty, out destinationProperty))
                {
                    destinationProperties[i] = destinationProperty;
                }
                else
                {
                    destinationProperties[i] = sourceProperty;
                }
            }

            return(new PropertyDescriptorCollection(destinationProperties));
        }
        internal DataItemPropertyDescriptorBase CreatePropertyDescriptor(PropertyDescriptor source)
        {
            if (!DataItemPropertyDescriptorFactory.IsReflected(source))
            {
                return(null);
            }

            var componentType = source.ComponentType;

            PropertyInfo propertyInfo;

            try
            {
                propertyInfo = componentType.GetProperty(source.Name, BindingFlags.Public | BindingFlags.Instance);
                if ((propertyInfo == null) || DataItemPropertyDescriptorFactory.IsIndexed(propertyInfo))
                {
                    return(null);
                }
            }
            catch (AmbiguousMatchException)
            {
                return(null);
            }

            var propertyType = propertyInfo.PropertyType;

            Debug.Assert(propertyType == source.PropertyType);

            var creatorHelper = DelegateCreatorHelperFactory.CreateHelper(componentType, propertyType);

            var getter = DataItemPropertyDescriptorFactory.CreateGetter(propertyInfo, creatorHelper);

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

            var setter   = DataItemPropertyDescriptorFactory.CreateSetter(propertyInfo, creatorHelper);
            var resetter = DataItemPropertyDescriptorFactory.CreateResetter(componentType, propertyInfo, creatorHelper);

            var propertyChangedEvent = this.GetPropertyChangedEvent(propertyInfo.Name);

            if (propertyChangedEvent != null)
            {
                return(new ValueChangeDataItemPropertyDescriptor(m_typeDescriptor, source, componentType, propertyType, getter, setter, resetter, propertyChangedEvent));
            }

            return(new DataItemPropertyDescriptor(m_typeDescriptor, source, componentType, propertyType, getter, setter, resetter));
        }