/// <summary> /// 获取本身是一个类的列表,但是数据比较少;(没办法确定父级结构的) /// </summary> /// <returns></returns> private static string GetSplitStrList(object data, VarClass varClass, SheetClass sheetClass) { string split = varClass.SplitStr; string classSplit = sheetClass.SplitStr; string str = ""; if (string.IsNullOrEmpty(split) || string.IsNullOrEmpty(classSplit)) { Debug.LogError("类的列类分隔符或变量分隔符为空!!!"); return(str); } object dataList = GetMemberValue(data, varClass.Name); int listCount = System.Convert.ToInt32(dataList.GetType().InvokeMember("get_Count", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { })); for (int i = 0; i < listCount; i++) { object item = dataList.GetType().InvokeMember("get_Item", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { i }); for (int j = 0; j < sheetClass.VarList.Count; j++) { object value = GetMemberValue(item, sheetClass.VarList[j].Name); str += value.ToString(); if (j != sheetClass.VarList.Count - 1) { str += classSplit.Replace("\\n", "\n").Replace("\\r", "\r"); } } if (i != listCount - 1) { str += split.Replace("\\n", "\n").Replace("\\r", "\r"); } } return(str); }
private static void ReadDataToClass(object objClass, SheetClass sheetClass, SheetData sheetData, Dictionary <string, SheetClass> allSheetClassDic, Dictionary <string, SheetData> sheetDataDic, object keyValue) { object item = CreateClass(sheetClass.Name);//只是为了得到变量类型 object list = CreateList(item.GetType()); for (int i = 0; i < sheetData.AllData.Count; i++) { if (keyValue != null && !string.IsNullOrEmpty(sheetData.AllData[i].ParnetVlue)) { if (sheetData.AllData[i].ParnetVlue != keyValue.ToString()) { continue; } } object addItem = CreateClass(sheetClass.Name); for (int j = 0; j < sheetClass.VarList.Count; j++) { VarClass varClass = sheetClass.VarList[j]; if (varClass.Type == "list" && string.IsNullOrEmpty(varClass.SplitStr)) { ReadDataToClass(addItem, allSheetClassDic[varClass.ListSheetName], sheetDataDic[varClass.ListSheetName], allSheetClassDic, sheetDataDic, GetMemberValue(addItem, sheetClass.MainKey)); } else if (varClass.Type == "list") { string value = sheetData.AllData[i].RowDataDic[sheetData.AllName[j]]; SetSplitClass(addItem, allSheetClassDic[varClass.ListSheetName], value); } else if (varClass.Type == "listStr" || varClass.Type == "listFloat" || varClass.Type == "listInt" || varClass.Type == "listBool" || varClass.Type == "listUshort") { string value = sheetData.AllData[i].RowDataDic[sheetData.AllName[j]]; SetSplitBaseClass(addItem, varClass, value); } else { string value = sheetData.AllData[i].RowDataDic[sheetData.AllName[j]]; if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(varClass.DefaultValue)) { value = varClass.DefaultValue; } if (string.IsNullOrEmpty(value)) { Debug.LogError("表格中有空数据,或者Reg文件未配置defaultValue!" + sheetData.AllName[j]); continue; } SetValue(addItem.GetType().GetProperty(sheetData.AllName[j]), addItem, value, sheetData.AllType[j]); } } list.GetType().InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, list, new object[] { addItem }); } objClass.GetType().GetProperty(sheetClass.ParentVar.Name).SetValue(objClass, list); }
/// <summary> /// 基础List赋值 /// </summary> /// <param name="objClass"></param> /// <param name="varClass"></param> /// <param name="value"></param> private static void SetSplitBaseClass(object objClass, VarClass varClass, string value) { Type type = null; if (varClass.Type == "listStr") { type = typeof(string); } else if (varClass.Type == "listFloat") { type = typeof(float); } else if (varClass.Type == "listInt") { type = typeof(int); } else if (varClass.Type == "listBool") { type = typeof(bool); } else if (varClass.Type == "listUshort") { type = typeof(ushort); } object list = CreateList(type); string[] rowArray = value.Split(new string[] { varClass.SplitStr }, StringSplitOptions.None); for (int i = 0; i < rowArray.Length; i++) { object addItem = rowArray[i].Trim(); try { list.GetType().InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, list, new object[] { addItem }); } catch { Debug.Log(varClass.ListSheetName + " 里 " + varClass.Name + " 列表添加失败!具体数值是:" + addItem); } } objClass.GetType().GetProperty(varClass.Name).SetValue(objClass, list); }
/// <summary> /// 获取基础List里面的所有值 /// </summary> /// <returns></returns> private static string GetSpliteBaseList(object data, VarClass varClass) { string str = ""; if (string.IsNullOrEmpty(varClass.SplitStr)) { Debug.LogError("基础List的分隔符为空!"); return(str); } object dataList = GetMemberValue(data, varClass.Name); int listCount = System.Convert.ToInt32(dataList.GetType().InvokeMember("get_Count", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { })); for (int i = 0; i < listCount; i++) { object item = dataList.GetType().InvokeMember("get_Item", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { i }); str += item.ToString(); if (i != listCount - 1) { str += varClass.SplitStr.Replace("\\n", "\n").Replace("\\r", "\r"); } } return(str); }
/// <summary> /// 递归读取配置 /// </summary> /// <param name="xe"></param> private static void ReadXmlNode(XmlElement xmlElement, Dictionary <string, SheetClass> allSheetClassDic, int depth) { depth++; foreach (XmlNode node in xmlElement.ChildNodes) { XmlElement xe = (XmlElement)node; if (xe.GetAttribute("type") == "list") { XmlElement listEle = (XmlElement)node.FirstChild; VarClass parentVar = new VarClass() { Name = xe.GetAttribute("name"), Type = xe.GetAttribute("type"), Col = xe.GetAttribute("col"), DefaultValue = xe.GetAttribute("defaultValue"), Foregin = xe.GetAttribute("foregin"), SplitStr = xe.GetAttribute("split"), }; if (parentVar.Type == "list") { parentVar.ListName = ((XmlElement)xe.FirstChild).GetAttribute("name"); parentVar.ListSheetName = ((XmlElement)xe.FirstChild).GetAttribute("sheetname"); } SheetClass sheetClass = new SheetClass() { Name = listEle.GetAttribute("name"), SheetName = listEle.GetAttribute("sheetname"), SplitStr = listEle.GetAttribute("split"), MainKey = listEle.GetAttribute("mainKey"), ParentVar = parentVar, Depth = depth, }; if (!string.IsNullOrEmpty(sheetClass.SheetName)) { if (!allSheetClassDic.ContainsKey(sheetClass.SheetName)) { //获取该类下面所有变量 foreach (XmlNode insideNode in listEle.ChildNodes) { XmlElement insideXe = (XmlElement)insideNode; VarClass varClass = new VarClass() { Name = insideXe.GetAttribute("name"), Type = insideXe.GetAttribute("type"), Col = insideXe.GetAttribute("col"), DefaultValue = insideXe.GetAttribute("defaultValue"), Foregin = insideXe.GetAttribute("foregin"), SplitStr = insideXe.GetAttribute("split"), }; if (varClass.Type == "list") { varClass.ListName = ((XmlElement)insideXe.FirstChild).GetAttribute("name"); varClass.ListSheetName = ((XmlElement)insideXe.FirstChild).GetAttribute("sheetname"); } sheetClass.VarList.Add(varClass); } allSheetClassDic.Add(sheetClass.SheetName, sheetClass); } } ReadXmlNode(listEle, allSheetClassDic, depth); } } }
/// <summary> /// 递归读取类里面的数据 /// </summary> /// <param name="data"></param> /// <param name="sheetClass"></param> /// <param name="allSheetClassDic"></param> /// <param name="sheetDataDic"></param> private static void ReadData(object data, SheetClass sheetClass, Dictionary <string, SheetClass> allSheetClassDic, Dictionary <string, SheetData> sheetDataDic, string mainKey) { List <VarClass> varList = sheetClass.VarList; VarClass varClass = sheetClass.ParentVar; object dataList = GetMemberValue(data, varClass.Name); int listCount = System.Convert.ToInt32(dataList.GetType().InvokeMember("get_Count", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { })); SheetData sheetData = new SheetData(); if (!string.IsNullOrEmpty(varClass.Foregin)) { sheetData.AllName.Add(varClass.Foregin); sheetData.AllType.Add(varClass.Type); } for (int i = 0; i < varList.Count; i++) { if (!string.IsNullOrEmpty(varList[i].Col)) { sheetData.AllName.Add(varList[i].Col); sheetData.AllType.Add(varList[i].Type); } } string tempKey = mainKey; for (int i = 0; i < listCount; i++) { object item = dataList.GetType().InvokeMember("get_Item", BindingFlags.Default | BindingFlags.InvokeMethod, null, dataList, new object[] { i }); RowData rowData = new RowData(); if (!string.IsNullOrEmpty(varClass.Foregin) && !string.IsNullOrEmpty(tempKey)) { rowData.RowDataDic.Add(varClass.Foregin, tempKey); } if (!string.IsNullOrEmpty(sheetClass.MainKey)) { mainKey = GetMemberValue(item, sheetClass.MainKey).ToString(); } for (int j = 0; j < varList.Count; j++) { if (varList[j].Type == "list" && string.IsNullOrEmpty(varList[j].SplitStr)) { SheetClass tempSheetClass = allSheetClassDic[varList[j].ListSheetName]; ReadData(item, tempSheetClass, allSheetClassDic, sheetDataDic, mainKey); } else if (varList[j].Type == "list") { SheetClass tempSheetClass = allSheetClassDic[varList[j].ListSheetName]; string value = GetSplitStrList(item, varList[j], tempSheetClass); rowData.RowDataDic.Add(varList[j].Col, value); } else if (varList[j].Type == "listStr" || varList[j].Type == "listFloat" || varList[j].Type == "listInt" || varList[j].Type == "listBool" || varList[j].Type == "listUshort") { string value = GetSpliteBaseList(item, varList[j]); rowData.RowDataDic.Add(varList[j].Col, value); } else { object value = GetMemberValue(item, varList[j].Name); if (varList != null) { rowData.RowDataDic.Add(varList[j].Col, value.ToString()); } else { Debug.LogError(varList[j].Name + "反射出来为空!"); } } } string key = varClass.ListSheetName; if (sheetDataDic.ContainsKey(key)) { sheetDataDic[key].AllData.Add(rowData); } else { sheetData.AllData.Add(rowData); sheetDataDic.Add(key, sheetData); } } }