/// <summary> /// 更新实体 /// </summary> /// <param name="container">容器</param> /// <param name="model">实体</param> /// <param name="type">命名类型</param> public static void UpdateModel(Control container, object model, PrefixType type) { Control.ControlCollection ctrs = container.Controls; if (ctrs == null || ctrs.Count <= 0) { return; } ClassInfoHandle classHandle = ClassInfoManager.GetClassHandle(model.GetType()); foreach (Control ctr in ctrs) { string proName = UpdateModelInfo.GetKey(ctr.Name, type); if (string.IsNullOrEmpty(proName)) { continue; } PropertyInfoHandle handle = classHandle.PropertyInfo[proName]; if (handle == null || !handle.HasSetHandle)//搜索里层 { UpdateModel(ctr, model, type); } else { try { SetModelValue(ctr, model, handle); } catch { } } } }
//private static Dictionary<string, Dictionary<string, ModelInfo>> dicCache = new Dictionary<string, Dictionary<string, ModelInfo>>(); /// <summary> /// 填充实体跟页面的对应表 /// </summary> /// <param name="objPage">页面类</param> /// <param name="modleType">实体类型</param> private static Dictionary <string, ModelInfo> GetPageMapInfo(Control objControl, Type modleType, PrefixType pType) { Dictionary <string, ModelInfo> handleMapping = new Dictionary <string, ModelInfo>(); ClassInfoHandle handle = ClassInfoManager.GetClassHandle(modleType); List <Control> lstCtrs = GetAllContorl(objControl); foreach (Control ctr in lstCtrs) { string id = UpdateModelInfo.GetKey(ctr.ID, pType); if (!string.IsNullOrEmpty(id)) { ModelInfo info = new ModelInfo(); PropertyInfoHandle pInfo = handle.PropertyInfo[id]; if (pInfo != null) { info.Phandle = pInfo; ContorlDefaultPropertyInfo ctrInfo = ControlDefaultValue.GetDefaultPropertyInfoWithoutCache(ctr); info.CtrHandle = ctrInfo; info.Ctr = ctr; handleMapping[ctr.ID] = info; } } } return(handleMapping); }
/// <summary> /// 获取类型所属的信息 /// </summary> /// <param name="objType">类型</param> /// <returns></returns> private static List <EntitySerializerInfo> GetTypeInfos(Type objType) { List <EntitySerializerInfo> lstInfos = new List <EntitySerializerInfo>(12); ClassInfoHandle classInfo = ClassInfoManager.GetClassHandle(objType); foreach (PropertyInfoHandle handle in classInfo.PropertyInfo) { EntitySerializerInfo info = new EntitySerializerInfo(); info.PropertyInfos.Add(handle); info.Name = handle.PropertyName; info.PropertyName = handle.PropertyName; lstInfos.Add(info); } return(lstInfos); }
/// <summary> /// 从流中读取XML /// </summary> /// <param name="stm">流</param> /// <returns></returns> public static List <T> ReadXML <T>(Stream stm, PageContent objPage) { stm.Position = 0; Type type = typeof(T); List <T> lst = new List <T>(); XmlDocument doc = new XmlDocument(); doc.Load(stm); if (objPage != null) //读取分页信息 { XmlNodeList rootList = doc.GetElementsByTagName("root"); if (rootList.Count > 0) { XmlAttribute attTotalPage = rootList[0].Attributes["totalPage"]; XmlAttribute attCurrentPage = rootList[0].Attributes["currentPage"]; XmlAttribute attPageSize = rootList[0].Attributes["pageSize"]; if (attTotalPage != null) { objPage.TotalPage = Convert.ToInt32(attTotalPage.InnerText); } if (attCurrentPage != null) { objPage.CurrentPage = Convert.ToInt32(attCurrentPage.InnerText); } if (attPageSize != null) { objPage.PageSize = Convert.ToInt32(attPageSize.InnerText); } } } ClassInfoHandle entityInfo = null; if (!type.IsValueType) { entityInfo = ClassInfoManager.GetClassHandle(type); } XmlNodeList nodeList = doc.GetElementsByTagName("item"); foreach (XmlNode node in nodeList) { if (entityInfo != null) { T obj = (T)entityInfo.CreateInstance(); foreach (XmlNode itemNode in node.ChildNodes) { string tagName = itemNode.Name; string value = itemNode.InnerText; PropertyInfoHandle info = entityInfo.PropertyInfo[tagName]; if (info != null) { Type resType = info.PropertyType; object curValue = StringToValue(value, resType); //转换成对象的值 info.SetValue(obj, curValue); //赋值 } } lst.Add(obj); } else { T curValue = (T)StringToValue(node.InnerText, type);//转换成对象的值 lst.Add(curValue); } } return(lst); }
/// <summary> /// 把集合写入XML流 /// </summary> /// <param name="list">集合</param> /// <param name="stm">流</param> /// <param name="objPage">分页</param> /// <param name="encoding">编码</param> public static void WriteXml(IList list, Stream stm, PageContent objPage, Encoding encoding) { if (list.Count <= 0) { return; } Type type = list[0].GetType(); ClassInfoHandle entityInfo = null; if (!type.IsValueType) { entityInfo = ClassInfoManager.GetClassHandle(type); } //Dictionary<string,EntityPropertyInfo>.Enumerator enuEntityProInfo=entityInfo.PropertyInfo.GetPropertyEnumerator(); XmlDocument doc = new XmlDocument(); string strxmlBase = ""; strxmlBase += "<?xml version=\"1.0\" encoding=\"" + encoding.BodyName + "\" ?>"; strxmlBase += "<root></root>"; doc.LoadXml(strxmlBase); XmlNode rootNode = doc.GetElementsByTagName("root")[0]; foreach (object obj in list) { XmlElement ele = doc.CreateElement("item"); if (entityInfo != null) { foreach (PropertyInfoHandle info in entityInfo.PropertyInfo) { if (info.PropertyType.IsValueType || info.PropertyType == typeof(string) || info.PropertyType == typeof(Nullable <>)) { XmlElement eleItem = doc.CreateElement(info.PropertyName); object value = info.GetValue(obj); string curValue = ValueToString(value); eleItem.InnerText = curValue; ele.AppendChild(eleItem); } } } else { ele.InnerText = obj.ToString(); } rootNode.AppendChild(ele); } if (objPage != null) //写入分页信息 { XmlAttribute attTotalPage = doc.CreateAttribute("totalPage"); attTotalPage.InnerText = objPage.TotalPage.ToString(); rootNode.Attributes.Append(attTotalPage); XmlAttribute attCurrentPage = doc.CreateAttribute("currentPage"); attCurrentPage.InnerText = objPage.CurrentPage.ToString(); rootNode.Attributes.Append(attCurrentPage); XmlAttribute attPageSize = doc.CreateAttribute("pageSize"); attPageSize.InnerText = objPage.PageSize.ToString(); rootNode.Attributes.Append(attPageSize); } doc.Save(stm); }