//通过XML Node 的 Attribute 来设置值。 private void fillEntityValueByXmlAttribute(object entity, XmlNode xmlNode) { PropertyInfo[] pros = entity.GetType().GetProperties(); foreach (PropertyInfo info in pros) { if (info.IsSpecialName || !info.CanWrite) { continue; } PropertyXmlConfigAttribute att = Attribute.GetCustomAttribute(info, typeof(PropertyXmlConfigAttribute)) as PropertyXmlConfigAttribute; if (att == null || !att.Switch) { continue; } string attName = string.IsNullOrEmpty(att.MappingName) ? info.Name : att.MappingName; if (info.PropertyType.IsValueType || string.Compare(info.PropertyType.Name, "String", true) == 0) { if (xmlNode.Attributes[attName] == null) { continue; } object val = xmlNode.Attributes[attName].Value.Trim(); if (info.PropertyType.IsEnum) { val = Enum.Parse(info.PropertyType, val.ToString()); } MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, info.Name, val); } else { XmlNode refrenceNode = getNodeByNodeName(xmlNode.ChildNodes, attName); if (refrenceNode != null) { setRefrenceEntityValue(entity, info, att, refrenceNode); } } } }
//设置引用类型的值 private void setRefrenceEntityValue(object entity, PropertyInfo info, PropertyXmlConfigAttribute att, XmlNode xmlNode) { //判断是否引用集合 if (info.PropertyType.GetInterface("IList") != null) { if (att.ReferenceModelType == null) { return; } object value = info.GetValue(entity, null); if (value == null) { return; } IList lstValue = value as IList; XmlNodeList childsNodes = null; if (att.NotExistsGroupNode) { childsNodes = xmlNode.ParentNode.ChildNodes; } else { childsNodes = xmlNode.ChildNodes; } if (childsNodes.Count == 0) { return; } string nodeName = string.IsNullOrEmpty(att.MappingName) ? info.Name : att.MappingName; foreach (XmlNode lstChild in childsNodes) { if (lstChild.NodeType != XmlNodeType.Element) { continue; } // if (string.Compare(lstChild.Name, nodeName, true) != 0) continue; object childEntity = DllFactory.Instance.CreateInstance(att.ReferenceModelType); lstValue.Add(childEntity); ModelXmlConfigAttribute t = Attribute.GetCustomAttribute(childEntity.GetType(), typeof(ModelXmlConfigAttribute)) as ModelXmlConfigAttribute; if (t == null) { MB.Util.TraceEx.Write(string.Format("类型{0} 没有配置ModelXmlConfigAttribute。", childEntity.GetType().FullName)); continue; } if (t.ByXmlNodeAttribute) { fillEntityValueByXmlAttribute(childEntity, lstChild); } else { fillEntityByXmlInnerText(childEntity, lstChild); } } } else { object childEntity = DllFactory.Instance.CreateInstance(info.PropertyType); MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, info.Name, childEntity); ModelXmlConfigAttribute t = Attribute.GetCustomAttribute(childEntity.GetType(), typeof(ModelXmlConfigAttribute)) as ModelXmlConfigAttribute; if (t == null) { MB.Util.TraceEx.Write(string.Format("类型{0} 没有配置ModelXmlConfigAttribute。", childEntity.GetType().FullName)); return; } if (t.ByXmlNodeAttribute) { fillEntityValueByXmlAttribute(childEntity, xmlNode); } else { fillEntityByXmlInnerText(childEntity, xmlNode); } } }
//通过InnerText 来设置实体对象属性的值。 private void fillEntityByXmlInnerText(object entity, XmlNode xmlNode) { PropertyInfo[] pros = entity.GetType().GetProperties(); foreach (PropertyInfo info in pros) { if (info.IsSpecialName || !info.CanWrite) { continue; } PropertyXmlConfigAttribute att = Attribute.GetCustomAttribute(info, typeof(PropertyXmlConfigAttribute)) as PropertyXmlConfigAttribute; if (att == null || !att.Switch) { continue; } string nodeName = string.IsNullOrEmpty(att.MappingName) ? info.Name : att.MappingName; XmlNode propertyNode = getNodeByNodeName(xmlNode.ChildNodes, nodeName); if (propertyNode == null) { continue; } if (info.PropertyType.IsValueType || string.Compare(info.PropertyType.Name, "String", true) == 0) { if (string.IsNullOrEmpty(propertyNode.InnerText)) { continue; } object val = propertyNode.InnerText.Trim(); //考虑到Enum 的值都是通过描述来配置的,所以这里需要特殊处理一下。 if (info.PropertyType.IsEnum) { val = Enum.Parse(info.PropertyType, val.ToString()); } MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, info.Name, val); } else if (info.PropertyType.IsArray) { string val = propertyNode.InnerText.Trim(); if (string.IsNullOrEmpty(val)) { continue; } if (string.Compare(info.PropertyType.Name, "String[]", true) == 0) { string[] aVals = val.Split(','); MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, info.Name, aVals); } else { throw new MB.Util.APPException("在 fillEntityByXmlInnerText 时,对于数组类型,目前只支持String[] 如果还需要其它的再追加"); } } else { XmlNode refrenceNode = getNodeByNodeName(xmlNode.ChildNodes, nodeName); if (refrenceNode != null) { setRefrenceEntityValue(entity, info, att, refrenceNode); } } } }