2018-10-17 22:54:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-10-19 00:36:32 +08:00
|
|
|
|
using System.Reflection;
|
2018-10-17 22:54:58 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace CoderEngine
|
|
|
|
|
{
|
|
|
|
|
public class AjaxCodeWrite
|
|
|
|
|
{
|
|
|
|
|
private string CodeStr = "";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 一个代码缩进
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string tabstr = " ";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 逐行添加代码
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="codeline"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public void CoderAddLine(string CodeLine)
|
|
|
|
|
{
|
|
|
|
|
CodeStr += CodeLine + "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 倍数缩进
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Number"></param>
|
|
|
|
|
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)
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-18 07:00:43 +08:00
|
|
|
|
TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
|
2018-10-19 00:36:32 +08:00
|
|
|
|
ModelToAjaxCodeWriter(tm);
|
2018-10-17 22:54:58 +08:00
|
|
|
|
return CodeStr;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ModelToAjaxCodeWriter(TypeMode TopMode)
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
CoderAddLine("");
|
|
|
|
|
CoderAddLine("//生成Ajax请求后解析返回值的js代码");
|
|
|
|
|
CoderAddLine("success: function(data) {//请求完成");
|
|
|
|
|
CoderAddLine(tabstr + "var ob = JSON.parse(data.Value);//序列化Value字符串为json对象");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
//如果有代码嵌套的下级类 在本类外 写入新的类
|
|
|
|
|
if (TopMode.TypeList != null && TopMode.TypeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine(tabstr);
|
2018-10-19 00:36:32 +08:00
|
|
|
|
AjaxCodeDownRecursion(TopMode,"",1);
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
2018-10-19 00:36:32 +08:00
|
|
|
|
CoderAddLine("}");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
public void AjaxCodeDownRecursion(TypeMode TopMode,string ParentCode, int TabNumber)
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
|
|
|
|
if (ParentCode == null || ParentCode == "")
|
|
|
|
|
ParentCode = "ob";
|
|
|
|
|
|
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
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(","));
|
2018-10-19 00:36:32 +08:00
|
|
|
|
ClassName = SonParamName.Substring(SonParamName.LastIndexOf(".") + 1);
|
2018-10-17 22:54:58 +08:00
|
|
|
|
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
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 + "的成员数量");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
if (tl.TypeList != null && tl.TypeList.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
CoderAddLine("");
|
2018-10-17 22:54:58 +08:00
|
|
|
|
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
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
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
ClassName = TopMode.MemberType.Name;
|
|
|
|
|
|
|
|
|
|
foreach (var tl in TopMode.TypeList)
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
if (tl.MemberType.Name == "List`1")
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
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));
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
/// <summary>
|
2018-10-19 00:36:32 +08:00
|
|
|
|
/// 转换成JS代码描述
|
2018-10-17 22:54:58 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2018-10-19 00:36:32 +08:00
|
|
|
|
public string CSharpToJavaTypeName(Type myType)
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
var oneinfo = CSharpToJSTypeItem.Where(w => w.Key == myType.Name).FirstOrDefault();
|
2018-10-17 22:54:58 +08:00
|
|
|
|
if (oneinfo.Value != null)
|
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
return "类型为 " + oneinfo.Value;
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-19 00:36:32 +08:00
|
|
|
|
if (myType.Name == "List`1")
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
else if (myType.Name == "Nullable`1")
|
2018-10-17 22:54:58 +08:00
|
|
|
|
{
|
|
|
|
|
var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
|
|
|
|
|
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
|
2018-10-19 00:36:32 +08:00
|
|
|
|
//string var = SonParamName.Substring(SonParamName.LastIndexOf('.') + 1);
|
|
|
|
|
string SonParamFromAssemblyName = SonParamName.Substring(0, SonParamName.IndexOf('.'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//貌似反射加载System是不行的?是吗
|
|
|
|
|
//Type PerentType = System.GetType(SonParamName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "";
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2018-10-19 00:36:32 +08:00
|
|
|
|
return "类型为 " + "string";
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
/// <summary>
|
2018-10-19 00:36:32 +08:00
|
|
|
|
/// C#和JS代码对照表
|
2018-10-17 22:54:58 +08:00
|
|
|
|
/// </summary>
|
2018-10-19 00:36:32 +08:00
|
|
|
|
public System.Collections.Generic.Dictionary<string, string> CSharpToJSTypeItem =
|
2018-10-17 22:54:58 +08:00
|
|
|
|
new Dictionary<string, string>() {
|
|
|
|
|
{ "int","int"},
|
|
|
|
|
{ "Int16","int"},
|
|
|
|
|
{ "Int32","int"},
|
|
|
|
|
{ "Int64","int"},
|
|
|
|
|
{ "long","long"},
|
|
|
|
|
{ "float","float"},
|
|
|
|
|
{ "double","double"},
|
|
|
|
|
{ "boolean","boolean"},
|
|
|
|
|
{ "string","string"},
|
2018-10-19 00:36:32 +08:00
|
|
|
|
{ "DateTime","时间格式形如 1999-99-99 10:10:10"},
|
2018-10-17 22:54:58 +08:00
|
|
|
|
};
|
2018-10-19 00:36:32 +08:00
|
|
|
|
|
2018-10-17 22:54:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|