Example #1
0
        /// <summary>
        ///     Returns all fields except that with Searchable(false) attribute.
        ///     obj != null, result != null
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IEnumerable <FieldInfo> GetAllFields(this object obj)
        {
            if (obj is null)
            {
                throw new ArgumentNullException(@"obj");
            }

            Type type   = obj.GetType();
            var  fields = new List <FieldInfo>();

            while (type != null)
            {
                foreach (var fieldInfo in type.GetFields(BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public |
                                                         BindingFlags.NonPublic))
                {
                    SearchableAttribute searchableAttribute = Attribute.GetCustomAttribute(fieldInfo, typeof(SearchableAttribute)) as SearchableAttribute;
                    if (searchableAttribute != null)
                    {
                        if (searchableAttribute.Searchable)
                        {
                            fields.Add(fieldInfo);
                        }
                        continue;
                    }

                    fields.Add(fieldInfo);
                }

                type = type.BaseType;
            }
            return(fields);
        }
Example #2
0
        /// <summary>
        ///     Returns all Browsable properties of object.
        ///     SearchableAttribute can explicitly set whether to return or not the property.
        /// </summary>
        private static IEnumerable <PropertyDescriptor> GetProperties(object obj)
        {
            var result = new List <PropertyDescriptor>();

            foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(obj)
                     .OfType <PropertyDescriptor>())
            {
                SearchableAttribute searchableAttribute =
                    propertyDescriptor.Attributes.OfType <SearchableAttribute>().FirstOrDefault();
                if (searchableAttribute != null)
                {
                    if (searchableAttribute.Searchable)
                    {
                        result.Add(propertyDescriptor);
                    }
                    continue;
                }
                if (propertyDescriptor.IsBrowsable)
                {
                    result.Add(propertyDescriptor);
                }
            }
            return(result);
        }