public void SetValue(XmlAttributeCollection inValue) { constructList.Add(new ItemConstruct()); // 构造函数 ItemConstruct construct = new ItemConstruct(new List<string>() { "System.String" }); construct.Struct.Statements.Add(Line("string[] ss", "inArg0.Split(\'^\')")); classer.CustomAttributes.Add(new CodeAttributeDeclaration("ProtoContract")); for (int i = 0; i < inValue.Count; i++) { fieldList.Add(new ItemField(inValue[i].Name, inValue[i].Value, MemberAttributes.Private)); ItemProperty item = new ItemProperty(inValue[i].Name); item.SetGetName(); item.SetSetName(); item.SetValueType(inValue[i].Value); item.SetModifier(MemberAttributes.Public | MemberAttributes.Final); item.SetField("ProtoMember", (i + 1).ToString()); propertyList.Add(item); Type t = Stringer.ToType(inValue[i].Value); string right = t == typeof(System.String) ? "ss[" + i + "]" : t == typeof(System.UInt32) ? "uint.Parse(ss[" + i + "])" : t == typeof(System.Single) ? "float.Parse(ss[" + i + "])" : "new " + t.ToString() + "(inValues[" + i + "])"; construct.Struct.Statements.Add(Line("_" + Stringer.FirstLetterLower(inValue[i].Name), right)); } constructList.Add(construct); Create(); }
public void SetValue(XmlAttributeCollection inValue) { constructList.Add(new ItemConstruct()); // 构造函数 ItemConstruct construct = new ItemConstruct(new List <string>() { "System.String" }); construct.Struct.Statements.Add(Line("string[] ss", "inArg0.Split(\'^\')")); classer.CustomAttributes.Add(new CodeAttributeDeclaration("ProtoContract")); for (int i = 0; i < inValue.Count; i++) { fieldList.Add(new ItemField(inValue[i].Name, inValue[i].Value, MemberAttributes.Private)); ItemProperty item = new ItemProperty(inValue[i].Name); item.SetGetName(); item.SetSetName(); item.SetValueType(inValue[i].Value); item.SetModifier(MemberAttributes.Public | MemberAttributes.Final); item.SetField("ProtoMember", (i + 1).ToString()); propertyList.Add(item); Type t = Stringer.ToType(inValue[i].Value); string right = t == typeof(System.String) ? "ss[" + i + "]" : t == typeof(System.UInt32) ? "uint.Parse(ss[" + i + "])" : t == typeof(System.Single) ? "float.Parse(ss[" + i + "])" : "new " + t.ToString() + "(inValues[" + i + "])"; construct.Struct.Statements.Add(Line("_" + Stringer.FirstLetterLower(inValue[i].Name), right)); } constructList.Add(construct); Create(); }
public void SetNodeValue(XmlNode n) { for (int i = 0; i < n.Attributes.Count; i++) { string value = n.Attributes[i].Value; //AddField(n.Attributes[i].Name, value, MemberAttributes.Private); fieldList.Add(new ItemField(n.Attributes[i].Name, value, MemberAttributes.Private)); ItemProperty item = new ItemProperty(n.Attributes[i].Name); item.SetGetName(); item.SetValueType(value); item.SetModifier(MemberAttributes.Public | MemberAttributes.Final); propertyList.Add(item); CodeConditionStatement condition = new CodeConditionStatement(); condition.Condition = new CodeVariableReferenceExpression("inArg0.ContainsKey(\"" + n.Attributes[i].Name + "\")"); string parseLeft = ""; string parseRight = ""; if (Stringer.IsNumber(value)) { parseLeft = value.Contains(".") ? "float.Parse(" : "uint.Parse("; parseRight = ")"; } CodeVariableReferenceExpression right = new CodeVariableReferenceExpression(parseLeft + "inArg0[\"" + n.Attributes[i].Name + "\"]" + parseRight); CodePropertyReferenceExpression left = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "_" + Stringer.FirstLetterLower(n.Attributes[i].Name)); if (Stringer.IsNumber(value)) { CodeConditionStatement numCondition = new CodeConditionStatement(); numCondition.Condition = new CodeVariableReferenceExpression("inArg0[\"" + n.Attributes[i].Name + "\"] == \"\""); numCondition.TrueStatements.Add(new CodeAssignStatement(left, new CodeVariableReferenceExpression("0"))); numCondition.FalseStatements.Add(new CodeAssignStatement(left, right)); condition.TrueStatements.Add(numCondition); } else { condition.TrueStatements.Add(new CodeAssignStatement(left, right)); } AddConditionStatement(condition); } Create(); }
/// <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(); }
/// <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(); }