private XElement ToXml(object obj, PropertyInfo pInfo) { if (pInfo == null) { return(null); } object[] atts = pInfo.GetCustomAttributes(typeof(XmlPersistAttribute), false); if (atts == null || atts.Length == 0) { return(ValueTypePropertyToXml(obj, pInfo)); } XmlPersistAttribute xlmPersistAtt = atts[0] as XmlPersistAttribute; IPropertyConverter convter = null; try { convter = Activator.CreateInstance(xlmPersistAtt.PropertyConverter) as IPropertyConverter; object propertyValue = obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null); if (propertyValue != null) { 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(XmlPersistAttribute), false); XmlPersistAttribute xmlPersisitAtt = atts[0] as XmlPersistAttribute; IPropertyConverter convter = Activator.CreateInstance(xmlPersisitAtt.PropertyConverter) as IPropertyConverter; object value = convter.FromXml(pElement); obj.GetType().InvokeMember(proName, BindingFlags.SetProperty, null, obj, new object[] { value }); }