private static void Match(PropertyInfo pInfo, XmlNode xmlNode, object context) { SaveKeyCodeAttribute Attribute = pInfo.GetCustomAttribute <SaveKeyCodeAttribute>(); if (Attribute != null) { foreach (XmlElement node in xmlNode.ChildNodes) { if (node.Name == Attribute.KeyCode) { SetUpData(node, pInfo, context); break; } } } }
private static KeyCodeDictionary CollectGlobalDataModel(object Data) { //inquiring global Type globalType = Data.GetType(); //get all properties - GDMPage01, 02, etc PropertyInfo[] GDMPropertiesInfo = globalType.GetProperties(BindingFlags.Instance | BindingFlags.Public); KeyCodeDictionary dictionary = new KeyCodeDictionary(); foreach (PropertyInfo propertyInfo in GDMPropertiesInfo) { //get the custom attribute that makes the object saveable SaveKeyCodeAttribute Attribute = propertyInfo.GetCustomAttribute <SaveKeyCodeAttribute>() as SaveKeyCodeAttribute; if (Attribute != null) { if (Attribute.SaveDataType == SaveDataType.Class) { /* * PropertyWithNavigation => * PropertyAttribute :: [Properties<Attribute, value>] */ var item = propertyInfo.GetValue(Data); object returnData = CollectGlobalDataModel(item); dictionary.Add(Attribute, returnData); } else if (Attribute.SaveDataType == SaveDataType.ListOfClass) { //convert current object to list IEnumerable <object> list = (IEnumerable <object>)propertyInfo.GetValue(Data); List <KeyCodeDictionary> convertedItems = new List <KeyCodeDictionary>(); /* * ListNavigation translates like: * List<SomeClass> = [Object1, Object2, Object3] is * ListAttributeKeyCode :: * [ * [ Object1Attribute, value == [ Attr1 :: val1; Attr2 :: val2 ] ], * [ Object2Attribute, value ], * [ Object3Attribute, value ] * ] * */ foreach (var item in list) { convertedItems.Add(CollectGlobalDataModel(item)); } dictionary.Add(Attribute, convertedItems); } else if (Attribute.SaveDataType == SaveDataType.List) { /* * List<int> = [1,2,3] is * ListAttrCode :: [1,2,3] ] */ object returnData = propertyInfo.GetValue(Data); dictionary.Add(Attribute, returnData); } else { //Property => Attribute :: Value dictionary.Add(Attribute, propertyInfo.GetValue(Data)); } } } return(dictionary); }
public static void SetUpData(XmlElement element, PropertyInfo pInfo, object Context) { SaveKeyCodeAttribute Attribute = pInfo.GetCustomAttribute <SaveKeyCodeAttribute>(); string KeyCode = Attribute.KeyCode; SaveDataType SaveDataType = Attribute.SaveDataType; Type CollectionType = Attribute.CollectionElementType; if (SaveDataType == SaveDataType.Class) { object PropertyValue = pInfo.GetValue(Context); //iterate through properties of this class PropertyInfo[] propertiesInfo = PropertyValue.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pInfoInContext in propertiesInfo) { Match(pInfoInContext, element, PropertyValue); } } else if (SaveDataType == SaveDataType.ListOfClass) { var data = pInfo.GetValue(Context); IList ocollection = (IList)data; //because we are working with the existing global - reset the collection ocollection.Clear(); //iterate through items of collection for (var i = 0; i < element.ChildNodes.Count; i++) { var classItem = SolveConstructor(CollectionType); if (classItem == null) { throw new Exception(string.Format("Unable to create class item {0}", CollectionType.FullName)); } ocollection.Add(classItem); } int index = 0; foreach (XmlElement xmlItem in element.ChildNodes) { var classItem = ocollection[index]; index++; //iterate through properties of this class PropertyInfo[] propertiesInfo = classItem.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pInfoInContext in propertiesInfo) { Match(pInfoInContext, xmlItem, classItem); } } } else if (SaveDataType == SaveDataType.List) { IList collection = (IList)pInfo.GetValue(Context); //because we are working with the existing global - reset the collection collection.Clear(); //iterate through items of collection foreach (XmlElement xmlItem in element.ChildNodes) { collection.Add(TrySolveConverter(xmlItem.InnerText, Attribute.CollectionElementType)); } } else if (SaveDataType == SaveDataType.Default) { string value = element.InnerText; object solvedValue = TrySolveConverter(value, pInfo.PropertyType); if (solvedValue == null) { throw new Exception("Unable to solve request"); } pInfo.SetValue(Context, solvedValue); } }