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 ItemProperty(string inName) { property = new CodeMemberProperty(); property.HasGet = false; property.HasSet = false; property.Name = Stringer.FirstLetterLower(inName); }
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> /// Set属性 /// </summary> /// <param name="inName"></param> public void SetSetName(string inName = "") { string s = inName == "" ? property.Name : inName; property.HasSet = true; property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "m_" + Stringer.FirstLetterLower(s)), new CodePropertySetValueReferenceExpression())); }
/// <summary> /// Get属性 /// </summary> /// <param name="inName"></param> public void SetGetName(string inName = "") { string s = inName == "" ? property.Name : inName; property.HasGet = true; property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "m_" + Stringer.FirstLetterLower(s)))); }
/// <summary> /// 字段 /// </summary> /// <param name="inName">字段名</param> /// <param name="inType">字段类型</param> /// <param name="inAtt">访问修饰符</param> public ItemField(string inName, string inType, MemberAttributes inAtt) { field = new CodeMemberField(Stringer.ToType(inType), "m_" + Stringer.FirstLetterLower(inName)); field.Attributes = inAtt; }
/// <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(); }