/// <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); }
/// <summary> /// Serialization method fills the tree /// </summary> /// <param name="type">Type of object to serialize</param> /// <param name="obj">The main object to serialize</param> /// <param name="itemTree">Tree to fill</param> private void Serialiaze(Type type, object obj, _ItemTree itemTree) { if (itemTree.ParentName == "") { itemTree.ParentName = "Master"; } itemTree.SelfName = type.Name.ToString(); _ExcludeMembers excludeMembers = new _ExcludeMembers(); itemTree.Collections = GetCollections(type, obj, excludeMembers); itemTree.Fields = GetFields(type, obj, excludeMembers); itemTree.Properties = GetProperties(type, obj, excludeMembers); Type subType; string assemblyName; foreach (_Item item in itemTree.Fields) { int dotIdx = item.itemValue.LastIndexOf("."); if (dotIdx >= 0) { assemblyName = item.itemValue.Substring(0, item.itemValue.LastIndexOf(".")); subType = Type.GetType(item.itemValue + ", " + assemblyName); if (subType != null) { itemTree.subTrees.Add(new _ItemTree(item.itemName)); FieldInfo field = GetFieldInfo(item.itemName, type); Serialiaze(subType, field.GetValue(obj), itemTree.subTrees[itemTree.subTrees.Count - 1]); } } } foreach (_Item item in itemTree.Properties) { int dotIdx = item.itemValue.LastIndexOf("."); if (dotIdx >= 0) { assemblyName = item.itemValue.Substring(0, item.itemValue.LastIndexOf(".")); subType = Type.GetType(item.itemValue + ", " + assemblyName); if (subType != null) { itemTree.subTrees.Add(new _ItemTree(item.itemName)); PropertyInfo prop = GetPropertyInfo(item.itemName, type); Serialiaze(subType, prop.GetValue(obj), itemTree.subTrees[itemTree.subTrees.Count - 1]); } } } foreach (_Collection collection in itemTree.Collections) { int i = 0; foreach (string item in collection.Items) { int dotIdx = item.LastIndexOf("."); if (dotIdx >= 0) { assemblyName = item.Substring(0, item.LastIndexOf(".")); subType = Type.GetType(item + ", " + assemblyName); if (subType != null) { itemTree.subTrees.Add(new _ItemTree(collection.collectionName)); object itemObject = GetObjectFromCollection(type, obj, collection.collectionName, i); Serialiaze(subType, itemObject, itemTree.subTrees[itemTree.subTrees.Count - 1]); } } i++; } } }