private static List <DottedFieldDescription> GetPropertyValueNames(Object obj) { List <DottedFieldDescription> valueNames = new List <DottedFieldDescription>(); Type type = obj.GetType(); PropertyInfo[] props = type.GetProperties(); foreach (var prop in props) { DottedFieldDescription currentDottedInformation = new DottedFieldDescription(); currentDottedInformation.Name = prop.Name; currentDottedInformation.Type = prop.PropertyType.Name; if (prop.PropertyType.IsPrimitive) { currentDottedInformation.Value = prop.GetValue(obj).ToString(); valueNames.Add(currentDottedInformation); } else if (!prop.PropertyType.IsGenericType) { if (prop.PropertyType.IsSealed) { currentDottedInformation.Value = prop.GetValue(obj).ToString(); valueNames.Add(currentDottedInformation); } else { currentDottedInformation.IsClass = true; valueNames.Add(currentDottedInformation); foreach (DottedFieldDescription info in GetPropertyValueNames(prop.GetValue(obj))) { info.Name = currentDottedInformation.Name + "." + info.Name; valueNames.Add(info); } } } else if (prop.PropertyType.IsGenericType) { currentDottedInformation.IsContainer = true; valueNames.Add(currentDottedInformation); foreach (DottedFieldDescription info in GetGenericValueNames(prop.GetValue(obj), currentDottedInformation)) { info.Name = currentDottedInformation.Name + info.Name; valueNames.Add(info); } } else if (prop.PropertyType.IsClass) { currentDottedInformation.IsClass = true; valueNames.Add(currentDottedInformation); foreach (DottedFieldDescription info in GetPropertyValueNames(prop.GetValue(obj))) { info.Name = currentDottedInformation.Name + "." + info.Name; valueNames.Add(info); } } } return(valueNames); }
private static List <DottedFieldDescription> GetGenericValueNames(Object obj, DottedFieldDescription parentInfo) { List <DottedFieldDescription> valueNames = new List <DottedFieldDescription>(); Type type = obj.GetType(); PropertyInfo count = type.GetProperty("Count"); if (count != null) { parentInfo.Value = count.GetValue(obj).ToString(); } MethodInfo enumerater = type.GetMethod("GetEnumerator"); if (enumerater != null) { int index = 0; IEnumerable myEnum = obj as IEnumerable; IEnumerator myEnumerator = myEnum.GetEnumerator(); while (myEnumerator.MoveNext()) { DottedFieldDescription currentInfo = new DottedFieldDescription(); currentInfo.Name = "_" + (index++).ToString(); Object currentElement = myEnumerator.Current; Type currentType = currentElement.GetType(); if (currentType.IsPrimitive || (!currentType.IsGenericType && currentType.IsSecurityTransparent)) { currentInfo.Type = currentType.Name; currentInfo.Value = currentElement.ToString(); valueNames.Add(currentInfo); } else { foreach (DottedFieldDescription info in GetPropertyValueNames(currentElement)) { info.Name = currentInfo.Name + "." + info.Name; valueNames.Add(info); } } } } return(valueNames); }