Пример #1
0
        /// <summary>
        /// Retourne la definition d'un bean.
        /// </summary>
        /// <param name="beanType">Type du bean.</param>
        /// <param name="metadataType">Type portant les compléments de description.</param>
        /// <param name="bean">Bean.</param>
        /// <returns>Description des propriétés.</returns>
        private BeanDefinition GetDefinitionInternal(Type beanType, Type metadataType, object bean)
        {
            Type descriptionType = metadataType;

            if (descriptionType == null)
            {
                descriptionType = beanType;
            }

            BeanDefinition definition;

            if (!_beanDefinitionDictionnary.TryGetValue(descriptionType, out definition))
            {
                TableAttribute table        = (TableAttribute)TypeDescriptor.GetAttributes(beanType)[typeof(TableAttribute)];
                string         contractName = (table == null) ? null : table.Name;

                object[] attrs       = beanType.GetCustomAttributes(typeof(ReferenceAttribute), false);
                bool     isReference = attrs.Length == 1;
                bool     isStatic    = isReference ? ((ReferenceAttribute)attrs[0]).IsStatic : false;
                BeanPropertyDescriptorCollection properties = CreateBeanPropertyCollection(beanType, metadataType, bean);

                definition = new BeanDefinition(beanType, properties, contractName, isReference, isStatic);
                if (bean == null && !typeof(ICustomTypeDescriptor).IsAssignableFrom(beanType))
                {
                    _beanDefinitionDictionnary[descriptionType] = definition;
                }
            }

            return(definition);
        }
Пример #2
0
        /// <summary>
        /// Constructeur.
        /// </summary>
        /// <param name="beanType">Type du bean.</param>
        /// <param name="properties">Collection de propriétés.</param>
        /// <param name="contractName">Nom du contrat (table).</param>
        /// <param name="isReference"><code>True</code> si le bean correspond à une liste de référence, <code>False</code> sinon.</param>
        /// <param name="isStatic"><code>True</code> si le bean correspond à une liste de référence statique, <code>False</code> sinon.</param>
        internal BeanDefinition(Type beanType, BeanPropertyDescriptorCollection properties, string contractName, bool isReference, bool isStatic)
        {
            this.BeanType     = beanType;
            this.Properties   = properties;
            this.ContractName = contractName;
            this.IsReference  = isReference;
            this.IsStatic     = isStatic;
            foreach (BeanPropertyDescriptor property in properties)
            {
                if (property.IsPrimaryKey)
                {
                    this.PrimaryKey = property;
                }

                if (property.IsDefault)
                {
                    this.DefaultProperty = property;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Crée la collection des descripteurs de propriétés.
        /// </summary>
        /// <param name="properties">PropertyDescriptors.</param>
        /// <param name="defaultProperty">Propriété par défaut.</param>
        /// <param name="beanType">Type du bean.</param>
        /// <param name="metadataProperties">Métadonnées.</param>
        /// <returns>Collection.</returns>
        private static BeanPropertyDescriptorCollection CreateCollection(PropertyDescriptorCollection properties, PropertyDescriptor defaultProperty, Type beanType, PropertyDescriptorCollection metadataProperties)
        {
            BeanPropertyDescriptorCollection coll = new BeanPropertyDescriptorCollection(beanType);

            for (int i = 0; i < properties.Count; i++)
            {
                PropertyDescriptor property = properties[i];

                KeyAttribute            keyAttr          = (KeyAttribute)property.Attributes[typeof(KeyAttribute)];
                DisplayAttribute        displayAttr      = (DisplayAttribute)property.Attributes[typeof(DisplayAttribute)];
                ReferencedTypeAttribute attr             = (ReferencedTypeAttribute)property.Attributes[typeof(ReferencedTypeAttribute)];
                ColumnAttribute         colAttr          = (ColumnAttribute)property.Attributes[typeof(ColumnAttribute)];
                DomainAttribute         domainAttr       = (DomainAttribute)property.Attributes[typeof(DomainAttribute)];
                RequiredAttribute       requiredAttr     = (RequiredAttribute)property.Attributes[typeof(RequiredAttribute)];
                TranslatableAttribute   translatableAttr = (TranslatableAttribute)property.Attributes[typeof(TranslatableAttribute)];
                Type[] genericArgumentArray = beanType.GetGenericArguments();

                string display = null;
                if (displayAttr != null)
                {
                    if (displayAttr.ResourceType != null && displayAttr.Name != null)
                    {
                        Dictionary <string, PropertyInfo> resourceProperties;
                        if (!_resourceTypeMap.TryGetValue(displayAttr.ResourceType, out resourceProperties))
                        {
                            resourceProperties = new Dictionary <string, PropertyInfo>();
                            _resourceTypeMap[displayAttr.ResourceType] = resourceProperties;

                            foreach (PropertyInfo p in displayAttr.ResourceType.GetProperties(BindingFlags.Public | BindingFlags.Static))
                            {
                                resourceProperties.Add(p.Name, p);
                            }
                        }

                        display = resourceProperties[displayAttr.Name].GetValue(null, null).ToString();
                    }
                    else
                    {
                        display = displayAttr.Name;
                    }
                }

                string memberName     = (colAttr == null) ? null : colAttr.Name;
                bool   isPrimaryKey   = keyAttr != null;
                bool   isRequired     = requiredAttr != null;
                bool   isTranslatable = translatableAttr != null;
                string domainName     = (domainAttr == null) ? null : domainAttr.Name;
                bool   isDefault      = property.Equals(defaultProperty) || (DefaultPropertyDefaultName.Equals(property.Name) && defaultProperty == null);
                Type   referenceType  = attr == null ? null : attr.ReferenceType;
                //// Type dtoType = genericArgumentArray.Length > 0 ? genericArgumentArray[0] : beanType;
                bool isBrowsable = property.IsBrowsable;
                bool isReadonly  = property.IsReadOnly;

                // Traitement des métadonnées.
                if (metadataProperties != null)
                {
                    PropertyDescriptor metadata = metadataProperties[property.Name];
                    if (metadata != null)
                    {
                        if (!metadata.IsBrowsable)
                        {
                            isBrowsable = false;
                        }

                        if (metadata.Attributes[typeof(RequiredAttribute)] != null)
                        {
                            isRequired = true;
                        }

                        if (metadata.Attributes[typeof(TranslatableAttribute)] != null)
                        {
                            isTranslatable = true;
                        }

                        ReadOnlyAttribute readonlyAttr = (ReadOnlyAttribute)metadata.Attributes[typeof(ReadOnlyAttribute)];
                        if (readonlyAttr != null && readonlyAttr.IsReadOnly)
                        {
                            isReadonly = true;
                        }
                    }
                }

                BeanPropertyDescriptor description = new BeanPropertyDescriptor(
                    property.Name,
                    memberName,
                    property.PropertyType,
                    display,
                    domainName,
                    isPrimaryKey,
                    isDefault,
                    isRequired,
                    referenceType,
                    isReadonly,
                    isBrowsable,
                    isTranslatable);
                if (domainName != null)
                {
                    DomainManager.Instance.GetDomain(description);
                }

                coll.Add(description);
            }

            return(coll);
        }