public void TestGetObjMappingName_定义多种特性() { AttributeHelper helper = new AttributeHelper(); string mappingName = helper.GetTypeMappingName(typeof(DataItem_MappingClass_OtherToo)); Assert.AreEqual("TestMappingName", mappingName); }
public void TestGetObjMappingName_没有定义映射() { AttributeHelper helper = new AttributeHelper(); string mappingName = helper.GetTypeMappingName(typeof(DataItem_MappingClass_NoneMapping)); Assert.AreEqual("", mappingName); }
public bool DoAnalyse(string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement element = doc.DocumentElement; string typeMappingName = helper.GetTypeMappingName(typeof(T)); foreach (XmlNode node in element.ChildNodes) { if (node.Name.Equals(typeMappingName, StringComparison.OrdinalIgnoreCase)) { T t = new T(); t.AnalyseXML(node.OuterXml); this.Add(t); } else if (node.Name.Equals("totalResults", StringComparison.OrdinalIgnoreCase)) { totalResultNum = int.Parse(node.InnerText); } } return(true); }
public bool DoAnalyse(string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement element = doc.DocumentElement; string typeMappingName = helper.GetTypeMappingName(this.GetType()); if (element.Name.Equals(typeMappingName, StringComparison.OrdinalIgnoreCase)) { List <PropertyInfo> properties = new List <PropertyInfo>(this.GetType().GetProperties()); foreach (XmlNode node in element.ChildNodes) { for (int i = properties.Count - 1; i >= 0; i--) { PropertyInfo property = properties[i]; string dataMappingName, listMappingName; helper.GetPropertyMappingName(property, out dataMappingName, out listMappingName); if (string.IsNullOrEmpty(listMappingName)) { if (node.Name.Equals(dataMappingName, StringComparison.OrdinalIgnoreCase)) { if (property.PropertyType == typeof(int)) { property.SetValue(this, int.Parse(node.InnerText), null); } else if (property.PropertyType == typeof(bool)) { property.SetValue(this, bool.Parse(node.InnerText), null); } else if (property.PropertyType == typeof(DateTime)) { property.SetValue(this, DateTime.Parse(node.InnerText), null); } else if (property.PropertyType == typeof(string)) { property.SetValue(this, node.InnerText, null); } else if (property.PropertyType.IsSubclassOf(typeof(TOPDataItem))) { TOPDataItem item = (TOPDataItem)property.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); item.AnalyseXML(node.OuterXml); property.SetValue(this, item, null); } else { throw new ArgumentException("不支持当前数据类型 - " + property.PropertyType.FullName); } properties.Remove(property); } } else { if (node.Name.Equals(listMappingName, StringComparison.OrdinalIgnoreCase)) { IAnalyseXML list = (IAnalyseXML)property.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); list.AnalyseXML(node.OuterXml); property.SetValue(this, list, null); } } } } return(true); } else { return(false); } }