private XElement ToXml(object obj, PropertyInfo pInfo) { if (pInfo == null) { return(null); } object[] atts = pInfo.GetCustomAttributes(typeof(GeoDo.Core.XmlPersistAttribute), true); if (atts == null || atts.Length == 0) { return(ValueTypePropertyToXml(obj, pInfo)); } GeoDo.Core.XmlPersistAttribute xmlPersistAtt = atts[0] as GeoDo.Core.XmlPersistAttribute; if (!xmlPersistAtt.IsNeedPersisted) { return(null); } IPropertyConverter convter = null; try { object propertyValue = obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null); if (propertyValue != null) { if (xmlPersistAtt.PropertyConverter != null) { convter = Activator.CreateInstance(xmlPersistAtt.PropertyConverter) as IPropertyConverter; } if (pInfo.PropertyType.IsArray) { XElement containerXml = new XElement(pInfo.Name); Array array = propertyValue as Array; GenerateCollectionItemXElement(pInfo, array as IList, convter, xmlPersistAtt, containerXml); return(containerXml); } else if (PropertyIsList(pInfo)) { XElement containerXml = new XElement(pInfo.Name); IList lst = propertyValue as IList; GenerateCollectionItemXElement(pInfo, lst, convter, xmlPersistAtt, containerXml); return(containerXml); } else { return(convter.ToXml(pInfo.Name, propertyValue)); } } return(null); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
private void ReadComplexProperty(XElement pElement, object obj) { string proName = pElement.Name.LocalName; PropertyInfo pInfo = obj.GetType().GetProperty(proName); object[] atts = pInfo.GetCustomAttributes(typeof(GeoDo.Core.XmlPersistAttribute), true); if (atts == null || atts.Length == 0) { return; } GeoDo.Core.XmlPersistAttribute xmlPersisitAtt = atts[0] as GeoDo.Core.XmlPersistAttribute; GeoDo.Core.XmlContextVarAttribute xmlContextVarAtt = ContextVarAttribute(pInfo); object value = null; if (xmlPersisitAtt.PropertyConverter != null) { IPropertyConverter convter = Activator.CreateInstance(xmlPersisitAtt.PropertyConverter) as IPropertyConverter; convter.HostObject = obj; value = convter.FromXml(pElement); } else { var eles = pElement.Elements(); var elesArray = eles.ToArray(); int count = eles.Count(); if (count > 0) { if (pInfo.PropertyType.IsArray) { Array arry = Array.CreateInstance(xmlPersisitAtt.CollectionItemType, count); for (int i = 0; i < count; i++) { arry.SetValue(FromXml(elesArray[i]), i); } value = arry; } else if (PropertyIsList(pInfo)) { Type type = typeof(List <>); Type type1 = type.MakeGenericType(xmlPersisitAtt.CollectionItemType); IList lst = Activator.CreateInstance(type1) as IList; for (int i = 0; i < count; i++) { lst.Add(FromXml(elesArray[i])); } value = lst; } else { value = FromXml(elesArray[0]); } } } // if (obj.GetType().GetMethod("set_" + proName) != null) { obj.GetType().InvokeMember(proName, BindingFlags.SetProperty, null, obj, new object[] { value }); } // if (xmlContextVarAtt != null) { Object2Xml.PersistContextEnv.Put(xmlContextVarAtt.VarName, value); } }