Esempio n. 1
0
        public static FieldInfo[] GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
        {
            FieldInfo[] fieldInfos = type.GetFields(bindingFlags);

            // If this class doesn't have a base, don't waste any time
            if (type.BaseType == typeof(object))
            {
                return(fieldInfos);
            }
            else
            {   // Otherwise, collect all types up to the furthest base class
                var currentType   = type;
                var fieldComparer = new FieldInfoComparer();
                var fieldInfoList = new HashSet <FieldInfo>(fieldComparer);
                if (fieldInfos != null && fieldInfos.Length > 0)
                {
                    fieldInfoList.UnionWith(fieldInfos);
                }

                while (currentType != null && currentType != typeof(object))
                {
                    fieldInfos = currentType.GetFields(bindingFlags);
                    if (fieldInfos != null && fieldInfos.Length > 0)
                    {
                        fieldInfoList.UnionWith(fieldInfos);
                    }
                    currentType = currentType.BaseType;
                }
                return(fieldInfoList.ToArray());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Private fields in base classes aren't found in normal reflection, so we need to recurse on base types
        /// </summary>
        private static IEnumerable <FieldInfo> GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
        {
            FieldInfo[] fieldInfos = type.GetFields(bindingFlags);

            // If this class doesn't have a base, don't waste any time
            if (type.BaseType == typeof(object))
            {
                return(fieldInfos);
            }

            // Otherwise, collect all types up to the furthest base class
            var currentType   = type;
            var fieldComparer = new FieldInfoComparer();
            var fieldInfoList = new HashSet <FieldInfo>(fieldInfos, fieldComparer);

            while (currentType != typeof(object) && currentType != null)
            {
                fieldInfos = currentType.GetFields(bindingFlags);
                fieldInfoList.UnionWith(fieldInfos);
                currentType = currentType.BaseType;
            }
            return(fieldInfoList);
        }