using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace CoderEngine { public class AjaxCodeWrite { private string CodeStr = ""; /// /// 一个代码缩进 /// private string tabstr = " "; /// /// 逐行添加代码 /// /// /// public void CoderAddLine(string CodeLine) { CodeStr += CodeLine + "\n"; } /// /// 倍数缩进 /// /// public string TabstrForNumber(int Number) { string str = ""; if (Number < 0) Number = 0; for (int i = 0; i < Number; i++) { str += tabstr; } return str; } public string ModelToAjaxCode(string _AssemblyName, string _TypeName) { TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName); ModelToAjaxCodeWriter(tm); return CodeStr; } public void ModelToAjaxCodeWriter(TypeMode TopMode) { CoderAddLine(""); CoderAddLine("//生成Ajax请求后解析返回值的js代码"); CoderAddLine("success: function(data) {//请求完成"); CoderAddLine(tabstr + "var ob = JSON.parse(data.Value);//序列化Value字符串为json对象"); //如果有代码嵌套的下级类 在本类外 写入新的类 if (TopMode.TypeList != null && TopMode.TypeList.Count > 0) { CoderAddLine(tabstr); AjaxCodeDownRecursion(TopMode,"",1); } CoderAddLine("}"); } public void AjaxCodeDownRecursion(TypeMode TopMode,string ParentCode, int TabNumber) { if (ParentCode == null || ParentCode == "") ParentCode = "ob"; string ClassName = ""; //如果自己就是集合类型 则取类型下名字 if (TopMode.MemberType.Name == "List`1") { //FullName的值为 "System.Collections.Generic.List`1[[ --集合内嵌类型名称--, CoderEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" //其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型” var SonParamName = TopMode.MemberType.FullName.Substring(TopMode.MemberType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1); CoderAddLine(TabstrForNumber(TabNumber-1) + "//循环取内嵌集合"+ ParentCode+ "数据"); CoderAddLine(TabstrForNumber(TabNumber-1) + "for(i" + TabNumber + " = 0;i" + TabNumber + " < " + ParentCode+ ".lenght;i" + TabNumber + "++){"); foreach (var tl in TopMode.TypeList) { if (tl.MemberType.Name == "List`1") { CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType)); CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i"+ TabNumber + "]." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量"); if (tl.TypeList != null && tl.TypeList.Count > 0) { CoderAddLine(""); ParentCode = ParentCode + "[i" + TabNumber + "]"; string ParentCodeStr = ""; if (ParentCode != null) ParentCodeStr = ParentCode + "." + tl.Name; else ParentCodeStr = "ob." + tl.Name; AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1); } } else { CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "[i" + TabNumber + "]." + tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType)); } } CoderAddLine(TabstrForNumber(TabNumber-1) + "}"); } else { ClassName = TopMode.MemberType.Name; foreach (var tl in TopMode.TypeList) { if (tl.MemberType.Name == "List`1") { CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ";//集合 " + CSharpToJavaTypeName(tl.MemberType)); CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "." + tl.Name + ".lenght;//集合" + tl.Name + "的成员数量"); if (tl.TypeList != null && tl.TypeList.Count > 0) { CoderAddLine(""); string ParentCodeStr = ""; if (ParentCode != null) ParentCodeStr = ParentCode + "." + tl.Name; else ParentCodeStr = "ob." + tl.Name; AjaxCodeDownRecursion(tl, ParentCodeStr, TabNumber + 1); } } else { CoderAddLine(TabstrForNumber(TabNumber) + ParentCode + "."+ tl.Name + ";//成员参数 " + CSharpToJavaTypeName(tl.MemberType)); } } } } /// /// 转换成JS代码描述 /// /// public string CSharpToJavaTypeName(Type myType) { var oneinfo = CSharpToJSTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault(); if (oneinfo.Value != null) { return "类型为 " + oneinfo.Value; } else { if (myType.Name == "List`1") { return ""; } else if (myType.Name == "Nullable`1") { var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); //string var = SonParamName.Substring(SonParamName.LastIndexOf('.') + 1); string SonParamFromAssemblyName = SonParamName.Substring(0, SonParamName.IndexOf('.')); //貌似反射加载System是不行的?是吗 //Type PerentType = System.GetType(SonParamName); return ""; } else return "类型为 " + "string"; } } /// /// C#和JS代码对照表 /// public System.Collections.Generic.Dictionary CSharpToJSTypeItem = new Dictionary() { { "int","int"}, { "Int16","int"}, { "Int32","int"}, { "Int64","int"}, { "long","long"}, { "float","float"}, { "double","double"}, { "boolean","boolean"}, { "string","string"}, { "DateTime","时间格式形如 1999-99-99 10:10:10"}, }; } }