// other method Dictionary <string, FieldInfoX> GenFieldInfos(DataRow header) { Dictionary <string, FieldInfoX> fieldInfos = new Dictionary <string, FieldInfoX>(); int idx = 0; foreach (object o in header.ItemArray) { idx++; if (!o.ToString().Contains("|")) { continue; } FieldInfoX fi = GetFieldInfo(o.ToString()); if (fi == null) { continue; } fi.index = idx - 1; try { fieldInfos.Add(fi.name, fi); } catch (ArgumentException) { Console.Write(fi.name + " 键重复"); throw new Exception(" "); } } return(fieldInfos); }
object GetValue(FieldInfoX fi, string v, Assembly typelib) { object ret = null; try { switch (fi.type) { case "float": ret = float.Parse(v); break; case "TFloat": ret = (int)(Math.Round(float.Parse(v) * 1000)); break; case "TTFloat": ret = (int)(Math.Round(float.Parse(v) * 1000000)); break; case "int32": ret = int.Parse(v); break; case "bool": ret = (v.ToLower() == "true" || v == "1"); break; case "string": ret = v; break; default: { string typename = ns_ + fi.type.Replace(".", "+"); Type[] tt = typelib.GetTypes(); Type t = typelib.GetType(typename); ret = Enum.Parse(t, v); } break; } } catch (Exception) { } return(ret); }
FieldInfoX GetFieldInfo(string content) { content = content.Replace("\r\n", "").Replace("\n", ""); Match match = regex.Match(content); if (match.Success) { string flag = match.Groups["flag"].Value; if (convType == ConvType.Server && flag == "|") { return(null); } if (convType == ConvType.Client && flag == "||") { return(null); } string name = match.Groups["name"].Value; string type = match.Groups["type"].Value; string desc = match.Groups["desc"].Value; string constraint = match.Groups["constraint"].Value; FieldInfoX fi = new FieldInfoX(); fi.name = name; fi.type = ConvertType(type); fi.desc = desc; fi.constraint = constraint; return(fi); } else { throw new Exception("标题格式错误: " + content); } }
void GenDatas() { if (fullLib == null) { return; } // .xlsx to .protodata.bytes Console.WriteLine("\n----------Gen Data----------\n"); foreach (ExcelData xd in excels) { string sheetName = xd.sheetName; DataTable table = xd.table; Dictionary <string, FieldInfoX> fieldInfos = xd.fieldInfos /*[convType_]*/; if (fieldInfos.Count == 0) { continue; } Console.WriteLine("Gen Data from " + xd.excelName + ":" + sheetName); string typename = ns + "." + sheetName; Type t = fullLib.GetType(typename); PropertyInfo[] properties = t.GetProperties(); ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes); List <IExtensible> list = new List <IExtensible>(); for (int i = 1; i < table.Rows.Count; i++) { DataRow dr = table.Rows[i]; IExtensible obj = ci.Invoke(new object[0]) as IExtensible; foreach (PropertyInfo pi in properties) { FieldInfoX fi = null; string v = null; object value; try { if (pi.Name.EndsWith("_raw")) { fi = fieldInfos[pi.Name.Substring(0, pi.Name.Length - 4)]; v = dr[fi.index].ToString(); value = v; } else { fi = fieldInfos[pi.Name]; v = dr[fi.index].ToString(); value = GetValue(fi, v, fullLib); if (fi.type == "TFloat") { Type tfloat = fullLib.GetType(ns + ".TFloat"); ConstructorInfo tfloat_ci = tfloat.GetConstructor(Type.EmptyTypes); IExtensible o_tfloat = tfloat_ci.Invoke(new object[0]) as IExtensible; PropertyInfo pi_v = tfloat.GetProperty("v"); pi_v.SetValue(o_tfloat, value, BindingFlags.SetProperty, null, null, null); value = o_tfloat; } else if (fi.type == "TTFloat") { Type ttfloat = fullLib.GetType(ns + ".TTFloat"); ConstructorInfo ttfloat_ci = ttfloat.GetConstructor(Type.EmptyTypes); IExtensible o_ttfloat = ttfloat_ci.Invoke(new object[0]) as IExtensible; PropertyInfo pi_v = ttfloat.GetProperty("v"); pi_v.SetValue(o_ttfloat, value, BindingFlags.SetProperty, null, null, null); value = o_ttfloat; } } // 一行空数据 if (0 == fi.index && "" == v.Trim()) { obj = null; break; } pi.SetValue(obj, value, BindingFlags.SetProperty, null, null, null); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(string.Format("{0}, 行: {1}, 列: {2}", ex.Message, i + 1, fi.desc)); Console.ResetColor(); } } if (null != obj) { list.Add(obj); } } FileStream fs = new FileStream(dataPath + sheetName + ".protodata.bytes", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(t.FullName); bw.Write(list.Count); foreach (IExtensible obj in list) { Serializer.NonGeneric.SerializeWithLengthPrefix(fs, obj, PrefixStyle.Fixed32, 0); } fs.Close(); } }