internal static PickConfig PickThese(string[] config) { PickConfig pickConfig = new PickConfig(true, true) { Picks = new List <Pick>() }; foreach (var item in config) { string[] subItems = item.Split('.'); switch (subItems.Length) { case 1: pickConfig.Picks.Add(new Pick(subItems[0], new PickConfig(true, true))); break; case 2: pickConfig.Picks.Add(new Pick(subItems[0], new PickConfig(false, true, new List <Pick> { new Pick(subItems[1]) }))); break; case 3: pickConfig.Picks.Add(new Pick(subItems[0], new PickConfig(true, true, new List <Pick> { new Pick(subItems[1], new PickConfig(false, true, new List <Pick> { new Pick(subItems[2]) })) }))); break; case 4: pickConfig.Picks.Add(new Pick(subItems[0], new PickConfig(true, true, new List <Pick> { new Pick(subItems[1], new PickConfig(true, true, new List <Pick> { new Pick(subItems[2], new PickConfig(false, true, new List <Pick> { new Pick(subItems[3]) })) })) }))); break; } } return(pickConfig); }
public static IDictionary <string, object> ToPickDictionary(this object obj, PickConfig objConfig) { var dictionary = new ExpandoObject() as IDictionary <string, object>; foreach (var pick in objConfig.Picks) { try { string propertyName = pick.Name; PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName); object propertyValue = propertyInfo.ResolveValue(obj); PickConfig propertyPickConfig = pick.PickConfig; if (propertyInfo.PropertyType == typeof(Dictionary <string, object>)) { dictionary.AddItem(propertyName, propertyValue); continue; } // Normal property if (propertyInfo.IsNormalProperty()) { dictionary.AddItem(propertyName, propertyValue); } // Object property else if (propertyInfo.IsObjectProperty()) { // Not concerned with the values. So if a property is null, instantiate. if (propertyValue == null) { propertyValue = Activator.CreateInstance(propertyInfo.PropertyType); } // Default behavior if (propertyPickConfig == null) { dictionary.AddItem(propertyName, null); } else { var flatObject = ToPickDictionary(propertyValue, propertyPickConfig); // Flattens if (propertyPickConfig.Flatten) { foreach (var kvp in flatObject) { dictionary.AddItem(propertyName + kvp.Key, kvp.Value); } } // Maintains hierarchy else { dictionary.AddItem(propertyName, flatObject); } } } // Collection property else if (propertyInfo.IsCollectionProperty()) { // Not concerned with the values. So if a property is null, instantiate. if (propertyValue == null) { propertyValue = Activator.CreateInstance(propertyInfo.PropertyType); } // Default behavior if (propertyPickConfig == null) { dictionary.AddItem(propertyName, null); } else { var propertiesCollection = ((System.Collections.IEnumerable)propertyValue).Cast <dynamic>(); List <IDictionary <string, object> > resultsCollection = new List <IDictionary <string, object> >(); foreach (var propertyObject in propertiesCollection) { resultsCollection.Add(ToPickDictionary(propertyObject, propertyPickConfig)); } dictionary.AddItem(propertyName, resultsCollection); } } } catch (Exception e) { throw new Exception("Something is wrong with pick " + pick.Name + " in " + obj.GetType().Name, e); } } if (objConfig.IncludeAllNormalProperties) { var propertyInfoArray = obj.GetType().GetProperties(); foreach (var propertyInfo in propertyInfoArray) { try { string propertyName = propertyInfo.Name; object propertyValue = propertyInfo.ResolveValue(obj); if (propertyInfo.IsNormalProperty()) { dictionary.AddItem(propertyName, propertyValue); } } catch (Exception e) { throw new Exception("Something is wrong with property " + propertyInfo.Name + " in " + obj.GetType().Name, e); } } } // PostPick Hook //try //{ // // What if hook is not found? // obj.GetType().GetMethod("PostPick", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(obj, new object[] { dictionary }); //} //catch (Exception e) //{ // throw new Exception("PostPick hook failed.", e); //} return(dictionary); }
public static ICollection <IDictionary <string, object> > ToPickDictionaryCollection(this IEnumerable <object> entities, PickConfig pickConfig) { var result = new List <IDictionary <string, object> >(); foreach (var entity in entities) { result.Add(entity.ToPickDictionary(pickConfig)); } return(result); }