Ejemplo n.º 1
0
        /// <summary>
        /// Returns the Category Tree created from the Category Attributes on the properties in the model.
        /// </summary>
        /// <returns></returns>
        private CategoryTree GetPropertyCategories()
        {
            CategoryTree categories = new CategoryTree();

            Model firstChild = null;

            this.childrenWithSameType = this.GetChildModelsWithSameType(this.model);
            if (childrenWithSameType != null)
            {
                firstChild = this.childrenWithSameType.First() as Model;
            }

            if (firstChild != null)
            {
                foreach (PropertyInfo property in firstChild.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
                {
                    // Properties must have a [Description], not be called Name, and be read/write.
                    bool hasDescription  = property.IsDefined(typeof(DescriptionAttribute), false);
                    bool includeProperty = hasDescription &&
                                           property.Name != "Name" &&
                                           property.CanRead &&
                                           property.CanWrite;

                    // Only allow lists that are double[], int[] or string[]
                    if (includeProperty && property.PropertyType.GetInterface("IList") != null)
                    {
                        includeProperty = property.PropertyType == typeof(double[]) ||
                                          property.PropertyType == typeof(int[]) ||
                                          property.PropertyType == typeof(string[]);
                    }

                    if (includeProperty)
                    {
                        // Those properties with a [Catagory] attribute
                        bool hasCategory = property.IsDefined(typeof(CategoryAttribute), false);
                        if (hasCategory)
                        {
                            //get the attribute data
                            CategoryAttribute catAtt = (CategoryAttribute)property.GetCustomAttribute(typeof(CategoryAttribute));

                            //add the category name to the list of category items
                            categories.AddCategoryToTree(catAtt.Category);
                            //add the subcategory name to the list of subcategories for the category
                            CategoryItem catItem = categories.FindCategoryInTree(catAtt.Category);
                            catItem.AddSubcategoryName(catAtt.Subcategory);
                        }
                        else
                        {
                            //If there is not [Category] attribute at all on the property in the model.
                            //Add it to the "Unspecified" Category, and "Unspecified" Subcategory

                            categories.AddCategoryToTree("Unspecified");
                            CategoryItem catItem = categories.FindCategoryInTree("Unspecified");
                            catItem.AddSubcategoryName("Unspecified");
                        }
                    }
                }
            }
            return(categories);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the Category Tree created from the Category Attributes on the properties in the model.
        /// </summary>
        /// <returns></returns>
        private CategoryTree GetPropertyCategories()
        {
            CategoryTree categories = new CategoryTree();
            IModel       modelToUse = ModelForProperties();

            if (modelToUse != null)
            {
                foreach (PropertyInfo property in modelToUse.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
                {
                    // Properties must have a [Description], not be called Name, and be read/write.
                    bool hasDescription  = property.IsDefined(typeof(DescriptionAttribute), false);
                    bool includeProperty = hasDescription &&
                                           property.Name != "Name" &&
                                           property.CanRead &&
                                           property.CanWrite;

                    // Only allow lists that are double[], int[] or string[]
                    if (includeProperty && property.PropertyType.GetInterface("IList") != null)
                    {
                        includeProperty = property.PropertyType == typeof(double[]) ||
                                          property.PropertyType == typeof(int[]) ||
                                          property.PropertyType == typeof(string[]);
                    }

                    if (includeProperty)
                    {
                        // Those properties with a [Catagory] attribute
                        bool hasCategory = property.IsDefined(typeof(CategoryAttribute), false);
                        if (hasCategory)
                        {
                            //get the attribute data
                            CategoryAttribute catAtt = (CategoryAttribute)property.GetCustomAttribute(typeof(CategoryAttribute));
                            // add the category name to the list of category items
                            // allow : separated list for multiple categories
                            int catIndex = 0;
                            foreach (var catLabel in catAtt.Category.Split(':'))
                            {
                                if (catLabel != "*")
                                {
                                    categories.AddCategoryToTree(catLabel);
                                    //add the subcategory name to the list of subcategories for the category
                                    CategoryItem catItem   = categories.FindCategoryInTree(catLabel);
                                    var          subLabels = catAtt.Subcategory.Split(':');
                                    if (subLabels.Length >= catIndex + 1)
                                    {
                                        if (subLabels[catIndex] != "*")
                                        {
                                            catItem.AddSubcategoryName(subLabels[catIndex]);
                                        }
                                    }
                                }
                                catIndex++;
                            }
                            //categories.AddCategoryToTree(catAtt.Category);
                            ////add the subcategory name to the list of subcategories for the category
                            //CategoryItem catItem = categories.FindCategoryInTree(catAtt.Category);
                            //catItem.AddSubcategoryName(catAtt.Subcategory);
                        }
                        else
                        {
                            //If there is not [Category] attribute at all on the property in the model.
                            //Add it to the "Unspecified" Category, and "Unspecified" Subcategory
                            categories.AddCategoryToTree("Unspecified");
                            CategoryItem catItem = categories.FindCategoryInTree("Unspecified");
                            catItem.AddSubcategoryName("Unspecified");
                        }
                    }
                }
            }
            return(categories);
        }