/// <summary> /// Gets collections from a specified object /// </summary> /// <param name="type">Type of object</param> /// <param name="searchObj">The main object to search in</param> /// <param name="excludeMembers">Members shouldn't be described (were described before)</param> /// <returns></returns> private List <_Collection> GetCollections(Type type, object searchObj, _ExcludeMembers excludeMembers) { List <_Collection> result = new List <_Collection>(); FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); foreach (FieldInfo field in fieldInfos) { if (field.Name.IndexOf("k__BackingField") == -1 && !excludeMembers.Contains(field.Name.ToString())) { object obj = field.GetValue(searchObj); if (obj is IEnumerable && !(obj is string)) { string CollectionName = field.Name; List <string> collectionItems = new List <string>(); foreach (var item in obj as IEnumerable) { collectionItems.Add(Convert.ToString(item)); } result.Add(new _Collection(CollectionName, collectionItems)); excludeMembers.Add(CollectionName); } } } foreach (PropertyInfo prop in propertyInfos) { if (prop.Name.IndexOf("k__BackingField") == -1 && !excludeMembers.Contains(prop.Name.ToString())) { object obj = prop.GetValue(searchObj); if (obj is IEnumerable && !(obj is string)) { string CollectionName = prop.Name; List <string> collectionItems = new List <string>(); foreach (var item in obj as IEnumerable) { collectionItems.Add((string)item); } result.Add(new _Collection(CollectionName, collectionItems)); excludeMembers.Add(CollectionName); } } } return(result); }
/// <summary> /// Gets properties from a specified object /// </summary> /// <param name="type">Type of object</param> /// <param name="searchObj">The main object to search in</param> /// <param name="excludeMembers">Members shouldn't be described (were described before)</param> /// <returns></returns> private List <_Item> GetProperties(Type type, object searchObj, _ExcludeMembers excludeMembers) { List <_Item> result = new List <_Item>(); PropertyInfo[] propInfos = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); foreach (PropertyInfo prop in propInfos) { if (prop.Name.IndexOf("k__BackingField") == -1 && !excludeMembers.Contains(prop.Name.ToString())) { if (!(searchObj is IEnumerable && !(searchObj is string))) { string FieldName = prop.Name; string FieldValue = Convert.ToString(prop.GetValue(searchObj)); result.Add(new _Item(FieldName, FieldValue)); excludeMembers.Add(FieldName); } } } return(result); }
/// <summary> /// Gets fields from a specified object /// </summary> /// <param name="type">Type of object</param> /// <param name="searchObj">The main object to search in</param> /// <param name="excludeMembers">Members shouldn't be described (were described before)</param> /// <returns></returns> private List <_Item> GetFields(Type type, object searchObj, _ExcludeMembers excludeMembers) { List <_Item> result = new List <_Item>(); FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); foreach (FieldInfo field in fieldInfos) { if (field.Name.IndexOf("k__BackingField") == -1 && !excludeMembers.Contains(field.Name.ToString())) { if (!(searchObj is IEnumerable && !(searchObj is string))) { string FieldName = field.Name; object FieldValue = field.GetValue(searchObj).ToString(); result.Add(new _Item(FieldName, FieldValue.ToString())); excludeMembers.Add(FieldName); } } } return(result); }