public ItemProperty(string inName) { property = new CodeMemberProperty(); property.HasGet = false; property.HasSet = false; property.Name = Stringer.FirstLetterUp(inName); }
public ToCSharpBase(string inSpace, string inClassName, string inFolderName) { spaceName = inSpace.Trim(); className = Stringer.FirstLetterUp(inClassName); folderName = inFolderName; classer = new CodeTypeDeclaration(className); classer.IsClass = true; }
public void SetValue(List <string> inNames) { for (int i = 0; i < inNames.Count; i++) { string classname = Stringer.FirstLetterUp(inNames[i]); ItemField field = new ItemField("Dictionary<string," + classname + ">", classname + "Dic", "new " + "Dictionary<string," + classname + ">()"); field.SetAttributes(MemberAttributes.Final | MemberAttributes.Public); field.AddAttributes("ProtoMember", i + 1); fieldList.Add(field); } // Create(); }
public void SetValue(List <string> inNames) { usingList.Add("ProtoBuf"); usingList.Add("System.Collections.Generic"); classer.CustomAttributes.Add(new CodeAttributeDeclaration("ProtoContract")); for (int i = 0; i < inNames.Count; i++) { string classname = Stringer.FirstLetterUp(inNames[i]); ItemField field = new ItemField("Dictionary<string," + classname + ">", classname + "Dic", "new " + "Dictionary<string," + classname + ">()"); field.SetAttributes(MemberAttributes.Final | MemberAttributes.Public); field.AddAttributes("ProtoMember", i + 1); fieldList.Add(field); } Create(); }
/// <summary> /// Excel -> 二进制文件 /// </summary> /// <param name="inPath"></param> /// <param name="inFolderName"></param> /// <returns></returns> public bool Bin(string inPath, string inFolderName) { Assist.CheckFolderExist(inFolderName); PBData data = new PBData(); Assist.GetObjPaths(".xls", inPath).ForEach(delegate(string path) { DataSet ds = Assist.ExcelToData(path); DataTable dt = ds.Tables[0]; string classname = Stringer.FirstLetterUp(dt.TableName); Type classType = Type.GetType(classname); FieldInfo fieldInfo = typeof(PBData).GetField(classname + "Dic"); IDictionary fieldValue = fieldInfo.GetValue(data) as IDictionary; if (classType != null) { for (int i = 4; i < dt.Rows.Count; i++) { string key = ""; // key List <string> ls = new List <string>(); for (int x = 0; x < dt.Columns.Count; x++) { ls.Add(dt.Rows[i][x].ToString()); if (dt.Rows[2][x].ToString().Split('+')[2] == "1")// 当前值为Key值的一部分 { key += dt.Rows[i][x].ToString() + "_"; } } key = key.Remove(key.Length - 1, 1); IProtoBufable value = Activator.CreateInstance(classType) as IProtoBufable;// value value.Set(ls); fieldValue.Add(key, value); } fieldInfo.SetValue(data, fieldValue); } }); #region SceneLayout 读取同级目录下的“Excel/SceneLayout/”内的所有 xml 文件,并将其数据写入 PBData Dictionary <string, XmlDocument> doc = Assist.GetXml(Assist.RootPath + "Excel/SceneLayout/"); foreach (KeyValuePair <string, XmlDocument> item in doc) { SceneLayout sl = new SceneLayout(); XmlNodeList xcc = item.Value.SelectSingleNode("Config").ChildNodes; for (int i = 0; i < xcc.Count; i++) { SceneLayoutItem sli = new SceneLayoutItem(); IProtoBufable xmlItemValue = new SceneLayoutItem() as IProtoBufable;// value List <string> xls = new List <string>(); for (int x = 0; x < xcc[i].Attributes.Count; x++) { xls.Add(xcc[i].Attributes[x].Value); } xmlItemValue.Set(xls); sl.item.Add(xmlItemValue as SceneLayoutItem); } data.SceneLayoutDic.Add(item.Key, sl); } #endregion using (var file = System.IO.File.Create("PBData")) { try { ProtoBuf.Serializer.Serialize(file, data); } catch (Exception e) { MainWindow.Show(e.ToString()); } } return(true); }
private void ForGen(string inPath, string outPath) { DirectoryInfo folder = new DirectoryInfo(inPath); FileSystemInfo[] files = folder.GetFileSystemInfos(); int length = files.Length; for (int index = 0; index < length; index++) { if (files[index] is DirectoryInfo) { //this.ForGen(files[index].FullName); } else { if (files[index].Name.EndsWith(".xlsx")) { //DataSet mResultSet = Assist.ExcelToDataSet(files[index].FullName); FileStream stream = File.Open(files[index].FullName, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet mResultSet = excelReader.AsDataSet(); //判断Excel文件中是否存在数据表 if (mResultSet.Tables.Count < 1) { return; } //默认读取第一个数据表 DataTable mSheet = mResultSet.Tables[0]; string tableName = mSheet.TableName; //读取数据表行数和列数 int rowCount = mSheet.Rows.Count; int colCount = mSheet.Columns.Count; FieldInfo _fieldDic = type.GetField(tableName + "Dic"); IDictionary _iDic = _fieldDic.GetValue(obj) as IDictionary; Type _chileType = assembly.GetType("HuanJueData." + tableName); for (int i = contentBeginIndex; i < rowCount; i++)//行 { object _childObj = Activator.CreateInstance(_chileType); PropertyInfo pro = null; List <string> mainKeyList = new List <string>(); for (int j = 0; j < colCount; j++)//列 { string strData = mSheet.Rows[typeIndex][j].ToString(); string[] strs = strData.Split('+'); string keyType = strs[0]; int isMainkey = int.Parse(strs[1]); string linkTableName = strs[2]; string key = mSheet.Rows[titleIndex][j].ToString(); pro = _chileType.GetProperty(Stringer.FirstLetterUp(key)); string value = mSheet.Rows[i][j].ToString(); if (keyType == "int") { pro.SetValue(_childObj, int.Parse(value)); } else if (keyType == "float") { pro.SetValue(_childObj, float.Parse(value)); } else if (keyType == "string") { pro.SetValue(_childObj, value.ToString()); } //是主键,或者是合成主键的一部分字段 if (isMainkey == 1) { mainKeyList.Add(value); } } //最多由3个字段组成主键 string MainKey = ""; if (mainKeyList.Count == 1) { MainKey = mainKeyList[0]; } else if (mainKeyList.Count == 2) { MainKey = mainKeyList[0] + "_" + mainKeyList[1]; } else if (mainKeyList.Count == 3) { MainKey = mainKeyList[0] + "_" + mainKeyList[1] + "_" + mainKeyList[2]; } _iDic.Add(MainKey, _childObj); } } } } }
/// <summary> /// /// </summary> /// <param name="inPath"></param> /// <param name="outPath"></param> private void ForGen(string inPath, string outPath) { List <string> listClassNames = new List <string>(); Assist.ClearDirFiles(outPath); DirectoryInfo folder = new DirectoryInfo(inPath); FileSystemInfo[] files = folder.GetFileSystemInfos(); int length = files.Length; for (int index = 0; index < length; index++) { if (files[index] is DirectoryInfo) { //this.ForGen(files[index].FullName); } else { if (files[index].Name.EndsWith(".xlsx")) { //DataSet mResultSet = Assist.ExcelToDataSet(files[index].FullName); FileStream stream = File.Open(files[index].FullName, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet mResultSet = excelReader.AsDataSet(); //判断Excel文件中是否存在数据表 if (mResultSet.Tables.Count < 1) { return; } //默认读取第一个数据表 DataTable mSheet = mResultSet.Tables[0]; string tableName = mSheet.TableName; string csName = m_itemHeader + Stringer.FirstLetterUp(tableName); listClassNames.Add(csName); CreateCSItemData csItemData = new CreateCSItemData(m_nameSpace, csName, m_outPath); //读取数据表行数和列数 int rowCount = mSheet.Rows.Count; int colCount = mSheet.Columns.Count; List <string> mainKeyList = new List <string>(); for (int j = 0; j < colCount; j++)//列 { string key = mSheet.Rows[titleIndex][j].ToString(); if (key == "") { Assist.Log("下标" + "(" + titleIndex + "," + j + ")" + "不能为空"); return; } string strData = mSheet.Rows[typeIndex][j].ToString(); string[] strs = strData.Split('+'); string keyType = strs[0]; int isMainkey = int.Parse(strs[1]); string linkTableName = strs[2]; ItemField itemField = new ItemField(key, keyType, MemberAttributes.Private); csItemData.AddFieldValue(itemField); ItemProperty itemProperty = new ItemProperty(key); itemProperty.SetGetName(); itemProperty.SetSetName(); itemProperty.SetComment(mSheet.Rows[summary][j].ToString()); itemProperty.SetValueType(keyType); itemProperty.SetModifier(MemberAttributes.Public | MemberAttributes.Final); itemProperty.SetField("ProtoMember", (j + 1).ToString()); csItemData.AddPropertyValue(itemProperty); //是主键,或者是合成主键的一部分字段 if (isMainkey == 1) { mainKeyList.Add(key); } //有链接的表 if (linkTableName != "0") { linkTableName = m_itemHeader + Stringer.FirstLetterUp(linkTableName); string subClassname = linkTableName; ItemMethod mis = new ItemMethod("Get" + subClassname, MemberAttributes.Final | MemberAttributes.Public, new List <string>() { "System.String" }); csItemData.SetComment("获取" + subClassname, mis.Method); mis.Method.ReturnType = new CodeTypeReference(subClassname); mis.Method.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("CfgData.Instance.m_PBData." + subClassname + "Dic[inArg0]"))); csItemData.AddMethodValue(mis); } } //最多由3个字段组成主键 string MainKey = ""; if (mainKeyList.Count == 1) { MainKey = mainKeyList[0] + ".ToString()"; } else if (mainKeyList.Count == 2) { MainKey = mainKeyList[0] + ".ToString() + \"_\" + this.m_" + mainKeyList[1] + ".ToString()"; } else if (mainKeyList.Count == 3) { MainKey = mainKeyList[0] + ".ToString() + \"_\" + this.m_" + mainKeyList[1] + ".ToString() + \"_\" + this.m_" + mainKeyList[2] + ".ToString()"; } ItemProperty itemProperty2 = new ItemProperty("MainKey"); itemProperty2.SetGetName(MainKey); itemProperty2.SetComment("主键"); itemProperty2.SetValueType("string"); itemProperty2.SetModifier(MemberAttributes.Public | MemberAttributes.Final); csItemData.AddPropertyValue(itemProperty2); csItemData.Create(); } } } CreatePBData final = new CreatePBData(m_nameSpace, m_PBData, m_outPath); final.SetValue(listClassNames); final.Create(); List <string> dyncUsingList = new List <string>(); dyncUsingList.Add("protobuf-net.dll"); dyncUsingList.Add("mscorlib.dll"); List <string> dyncCsFilesName = Assist.GetObjPaths(".cs", m_outPath); string csDataPoolPath = Path.Combine(Environment.CurrentDirectory, "CfgData.cs"); dyncCsFilesName.Add(csDataPoolPath); for (int i = 0; i < dyncCsFilesName.Count; i++) { dyncCsFilesName[i] = dyncCsFilesName[i].Replace("/", "\\"); } string dyncDllPath = Path.Combine(m_outPath, "CfgData.dll"); CompilerResults cr = DyncLibTool.CompileCSharpCode(dyncCsFilesName, dyncDllPath, dyncUsingList); //Assembly assembly = cr.CompiledAssembly; Assist.Log("Gen OK"); }
/// <summary> /// <para>0: 字段名</para> /// <para>1: a+i+0 指明类型及是否为Key值</para> /// <para>2: 注释</para> /// </summary> /// <param name="inList">0: 字段名</param> public void SetValue(List <string[]> inList) { ItemMethod mi = new ItemMethod("Set", MemberAttributes.Final | MemberAttributes.Public, new List <string>() { "List<string>" }); classer.CustomAttributes.Add(new CodeAttributeDeclaration("ProtoContract")); List <string> keyList = new List <string>();// key 值 for (int i = 0; i < inList.Count; i++) { string[] ss = inList[i][1].Split('+'); string typeString = ss[1]; CustumType ct = Stringer.StrToEnum <CustumType>(typeString, CustumType.None); if (ct != CustumType.None) // 基本类型或自定义类型 { //AddField(inList[i][0], inList[i][1], MemberAttributes.Private); fieldList.Add(new ItemField(inList[i][0], inList[i][1], MemberAttributes.Private)); ItemProperty item = new ItemProperty(inList[i][0]); item.SetGetName(); item.SetSetName(); item.SetComment(inList[i][2]); item.SetValueType(inList[i][1]); item.SetModifier(MemberAttributes.Public | MemberAttributes.Final); item.SetField("ProtoMember", (i + 1).ToString()); propertyList.Add(item); if (ss[2] == "1")// 如果该属性是类的Key值,则加入列表 { keyList.Add(inList[i][0]); } Type vType = Stringer.ToType(inList[i][1]); string left = "_" + Stringer.FirstLetterLower(inList[i][0]); CodeVariableReferenceExpression right = new CodeVariableReferenceExpression(); if (vType == typeof(System.String)) { right.VariableName = "inArg0[" + i + "]"; } else if (vType == typeof(System.UInt32)) { right.VariableName = "uint.Parse(inArg0[" + i + "])"; } else if (vType == typeof(System.Single)) { right.VariableName = "float.Parse(inArg0[" + i + "])"; } else { right.VariableName = "new " + vType.ToString() + "(inArg0[" + i + "])"; } CodeAssignStatement ass = new CodeAssignStatement(new CodeVariableReferenceExpression(left), right); mi.Method.Statements.Add(ass); } else // 从属表 { string subclassname = Stringer.FirstLetterUp(typeString); ItemMethod mis = new ItemMethod("Get" + subclassname, MemberAttributes.Final | MemberAttributes.Public, new List <string>() { "System.String", "PBData" }); SetComment("获取" + inList[i][2], mis.Method); mis.Method.ReturnType = new CodeTypeReference(subclassname); mis.Method.Statements.Add(new CodeMethodReturnStatement(new CodeArgumentReferenceExpression("inArg1." + subclassname + "Dic[this.key + \"_\" + inArg0]"))); methodList.Add(mis); } } // Key 属性 ItemProperty keyPropertyItem = new ItemProperty("key"); if (keyList.Count == 1) { keyPropertyItem.SetGetName(keyList[0] + ".ToString()"); } else if (keyList.Count == 2) { keyPropertyItem.SetGetName(keyList[0] + ".ToString() + \"_\" +" + keyList[1] + ".ToString()"); } else if (keyList.Count == 3) { keyPropertyItem.SetGetName(keyList[0] + ".ToString() + \"_\" +" + keyList[1] + ".ToString() + \"_\" +" + keyList[2] + ".ToString()"); } keyPropertyItem.SetValueType(); keyPropertyItem.SetModifier(MemberAttributes.Public | MemberAttributes.Final); keyPropertyItem.SetComment("类的Key值"); propertyList.Add(keyPropertyItem); methodList.Add(mi); Create(); }