private static XmlAttribute[] GetAttsFromObject(XmlDocument doc, XmlNode node, object obj, ILayout layout) { PropertyInfo[] pInfos = obj.GetType().GetProperties(); if (pInfos == null || pInfos.Length == 0) { return(null); } List <XmlAttribute> xmlAttList = new List <XmlAttribute>(); //获取类型的名字空间以及类型名称 XmlAttribute type = doc.CreateAttribute("type"); type.Value = obj.GetType().Namespace + ".dll" + ":" + obj.GetType().FullName; xmlAttList.Add(type); foreach (PropertyInfo pInfo in pInfos) { PersistAttribute patt = GetPersistattribute(pInfo); if (patt == null) { continue; } XmlAttribute xmlAtt = CreateXmlAttribute(doc, pInfo, patt, obj, layout); xmlAttList.Add(xmlAtt); } return(xmlAttList != null?xmlAttList.ToArray() : null); }
private static XmlAttribute CreateXmlAttribute(XmlDocument doc, PropertyInfo pInfo, PersistAttribute patt, object obj, ILayout layout) { XmlAttribute att = doc.CreateAttribute(pInfo.Name.ToLower()); //获取属性的类型 例如:"GeoDo.RSS.Layout.enumLayoutUnit:Pixel" // string illuminateName = pInfo.PropertyType.Name.ToString() + ":"; if (patt.AttType == enumAttType.ValueType) { if (obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null) != null) { att.Value = obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null).ToString(); } } else { if (obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null) != null) { if (pInfo.Name.ToLower() == "size" && layout.Unit == enumLayoutUnit.Pixel) { SizeF size = (SizeF)pInfo.GetValue(obj, null); att.Value = GetBinaryString(size); } else if (pInfo.Name.ToLower() == "location" && layout.Unit == enumLayoutUnit.Pixel) { PointF loc = (PointF)pInfo.GetValue(obj, null); att.Value = GetBinaryString(loc); } else { att.Value = GetBinaryString(obj.GetType().InvokeMember(pInfo.Name, BindingFlags.GetProperty, null, obj, null)); } } } return(att); }