Beispiel #1
0
        // Returns all categories implemented by this object - these are decorated with the [CategoryAttribute]
        internal List <string> GetCategories(object obj)
        {
            // get all properties that are shown
            Type objType = obj.GetType();
            List <PropertyData> props = GetProperties(objType).ToList();

            // get the list of categories
            List <string> categories = new List <string>();

            foreach (PropertyData prop in props)
            {
                categories.AddRange(prop.Categories);
            }
            categories = categories.Distinct().ToList();

            // order (if there is a CategoryOrder property)
            PropertyInfo piCat = ObjectSupport.TryGetProperty(objType, "CategoryOrder");

            if (piCat != null)
            {
                List <string> orderedCategories = (List <string>)piCat.GetValue(obj);
                List <string> allCategories     = new List <string>();
                // verify that all returned categories in the list of ordered categories actually exist
                foreach (var oCat in orderedCategories)
                {
                    if (categories.Contains(oCat))
                    {
                        allCategories.Add(oCat);
                    }
                    //else
                    //throw new InternalError("No properties exist in category {0} found in CategoryOrder for type {1}.", oCat, obj.GetType().Name);
                }
                // if any are missing, add them to the end of the list
                foreach (var cat in categories)
                {
                    if (!allCategories.Contains(cat))
                    {
                        allCategories.Add(cat);
                    }
                }
                categories = allCategories;
            }
            return(categories);
        }
        public object GetProperty(string name, Type type)
        {
            // get the user settings (for anonymous users create one)
            object userInfo = Manager.UserSettingsObject;

            if (userInfo == null)
            {
                return(null);
            }
            // get the requested property
            PropertyInfo pi = ObjectSupport.TryGetProperty(userInfo.GetType(), name);

            if (pi == null)
            {
                throw new InternalError("User setting {0} doesn't exist", name);
            }
            if (pi.PropertyType != type)
            {
                throw new InternalError("User setting {0} is not of the requested type {1}", name, type.FullName);
            }
            return(pi.GetValue(userInfo));
        }