From dd54964321552cde1bd523bf5cc8122bfcd853f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=9A=93=E6=9C=88?= <353374337@qq.com>
Date: Thu, 18 Oct 2018 07:00:43 +0800
Subject: [PATCH] 233
---
CoderEngine/AjaxCodeWrite.cs | 2 +-
CoderEngine/CSharpCodeWrite.cs | 110 ++++++++++++++++++
CoderEngine/CoderEngine.csproj | 5 +-
CoderEngine/GolangCodeWrite.cs | 11 ++
CoderEngine/JavaCodeWrite.cs | 8 +-
CoderEngine/SwiftCodeWrite.cs | 11 ++
.../{ModleReader.cs => _ModleReader.cs} | 28 ++---
UseEngineConsolo/Program.cs | 10 +-
8 files changed, 166 insertions(+), 19 deletions(-)
create mode 100644 CoderEngine/CSharpCodeWrite.cs
create mode 100644 CoderEngine/GolangCodeWrite.cs
create mode 100644 CoderEngine/SwiftCodeWrite.cs
rename CoderEngine/{ModleReader.cs => _ModleReader.cs} (77%)
diff --git a/CoderEngine/AjaxCodeWrite.cs b/CoderEngine/AjaxCodeWrite.cs
index 705a3cf..8a70d60 100644
--- a/CoderEngine/AjaxCodeWrite.cs
+++ b/CoderEngine/AjaxCodeWrite.cs
@@ -26,7 +26,7 @@ namespace CoderEngine
public string ModelToJavaCode(string _AssemblyName, string _TypeName)
{
- TypeMode tm = new ModleReader().ModelCheck(_AssemblyName, _TypeName);
+ TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm);
return CodeStr;
}
diff --git a/CoderEngine/CSharpCodeWrite.cs b/CoderEngine/CSharpCodeWrite.cs
new file mode 100644
index 0000000..bc15760
--- /dev/null
+++ b/CoderEngine/CSharpCodeWrite.cs
@@ -0,0 +1,110 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CoderEngine
+{
+ public class CSharpCodeWrite
+ {
+ private string CodeStr = "";
+
+ ///
+ /// 一个代码缩进
+ ///
+ private string tabstr = " ";
+
+ ///
+ /// 逐行添加代码
+ ///
+ ///
+ ///
+ public void CoderAddLine(string CodeLine)
+ {
+ CodeStr += CodeLine + "\n";
+ }
+
+ public string ModelToJavaCode(string _AssemblyName, string _TypeName)
+ {
+ TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
+ ModelToCSCodeWriter(tm);
+ return CodeStr;
+ }
+
+ public void ModelToCSCodeWriter(TypeMode TopMode)
+ {
+ //如果有代码嵌套的下级类 在本类外 写入新的类
+ if (TopMode.TypeList != null && TopMode.TypeList.Count > 0)
+ {
+ CoderAddLine(tabstr);
+ CSCodeDownRecursion(TopMode);
+ }
+ }
+
+
+ public void CSCodeDownRecursion(TypeMode TopMode)
+ {
+ 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);
+ }
+ else
+ ClassName = TopMode.MemberType.Name;
+
+ CoderAddLine("public class " + ClassName + " {");
+ foreach (var tl in TopMode.TypeList)
+ {
+ CoderAddLine(tabstr + "public " + ToTypeName(tl.MemberType) + " " + tl.Name + " { get; set; };");
+ }
+
+ CoderAddLine("}");
+
+
+ foreach (var tl in TopMode.TypeList)
+ {
+ if (tl.MemberType.Name == "List`1")
+ {
+ if (tl.TypeList != null && tl.TypeList.Count > 0)
+ {
+ CoderAddLine("");
+ ModelToCSCodeWriter(tl);
+ }
+ }
+
+ }
+
+ }
+
+ ///
+ /// 类型处理
+ ///
+ ///
+ public string ToTypeName(Type myType)
+ {
+ if (myType.Name == "List`1")
+ {
+ var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
+ SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
+ return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.') + 1) + ">";
+ }
+ else if (myType.Name == "Nullable`1")
+ {
+ var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
+ SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
+ return "Nullable<" + SonParamName.Substring(SonParamName.LastIndexOf('.') + 1) + ">";
+ }
+ else
+ return myType.Name;
+ }
+
+
+
+
+ }
+}
diff --git a/CoderEngine/CoderEngine.csproj b/CoderEngine/CoderEngine.csproj
index 2e6a385..013cb19 100644
--- a/CoderEngine/CoderEngine.csproj
+++ b/CoderEngine/CoderEngine.csproj
@@ -40,9 +40,12 @@
+
+
-
+
+
\ No newline at end of file
diff --git a/CoderEngine/GolangCodeWrite.cs b/CoderEngine/GolangCodeWrite.cs
new file mode 100644
index 0000000..ab06465
--- /dev/null
+++ b/CoderEngine/GolangCodeWrite.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CoderEngine
+{
+ class GolangCodeWrite
+ {
+ }
+}
diff --git a/CoderEngine/JavaCodeWrite.cs b/CoderEngine/JavaCodeWrite.cs
index 6b9d24e..b865a3f 100644
--- a/CoderEngine/JavaCodeWrite.cs
+++ b/CoderEngine/JavaCodeWrite.cs
@@ -26,7 +26,7 @@ namespace CoderEngine
public string ModelToJavaCode(string _AssemblyName, string _TypeName)
{
- TypeMode tm = new ModleReader().ModelCheck(_AssemblyName, _TypeName);
+ TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm);
return CodeStr;
}
@@ -102,6 +102,12 @@ namespace CoderEngine
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.')+1) + ">";
}
+ else if (myType.Name == "Nullable`1")
+ {
+ var SonParamName = myType.FullName.Substring(myType.FullName.IndexOf("[") + 2);
+ SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
+ return SonParamName.Substring(SonParamName.LastIndexOf('.')+1);
+ }
else
return "string";
}
diff --git a/CoderEngine/SwiftCodeWrite.cs b/CoderEngine/SwiftCodeWrite.cs
new file mode 100644
index 0000000..893cde5
--- /dev/null
+++ b/CoderEngine/SwiftCodeWrite.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CoderEngine
+{
+ class SwiftCodeWrite
+ {
+ }
+}
diff --git a/CoderEngine/ModleReader.cs b/CoderEngine/_ModleReader.cs
similarity index 77%
rename from CoderEngine/ModleReader.cs
rename to CoderEngine/_ModleReader.cs
index 9889d7a..d7b8cec 100644
--- a/CoderEngine/ModleReader.cs
+++ b/CoderEngine/_ModleReader.cs
@@ -44,7 +44,7 @@ namespace CoderEngine
///
/// 模型读取
///
- public class ModleReader
+ public class _ModleReader
{
public TypeMode TopType = new TypeMode();
@@ -59,25 +59,25 @@ namespace CoderEngine
///
public TypeMode ModelCheck(string _AssemblyName, string _TypeName)
{
- Console.WriteLine("");
- Console.WriteLine("====== C# 数据模型转Java实体类代码实现 ======");
- Console.WriteLine("");
- Console.WriteLine("读取程序集" + _AssemblyName + "下实体类" + _TypeName + "");
+ //Console.WriteLine("");
+ //Console.WriteLine("====== C# 数据模型转Java实体类代码实现 ======");
+ //Console.WriteLine("");
+ //Console.WriteLine("读取程序集" + _AssemblyName + "下实体类" + _TypeName + "");
//取得类型
Type PerentType = Assembly.Load(_AssemblyName).GetType(_AssemblyName + "." + _TypeName);
if (PerentType == null)
{
- Console.WriteLine("读取程序集或类不存在");
+ //Console.WriteLine("读取程序集或类不存在");
}
- Console.WriteLine("已成功反射获取程序集");
+ //Console.WriteLine("已成功反射获取程序集");
TopType.MemberType = PerentType;
- Console.WriteLine("对象类型:"+ TopType.MemberType);
+ //Console.WriteLine("对象类型:"+ TopType.MemberType);
TopType.Name = PerentType.Name;
- Console.WriteLine("对象名称:" + TopType.Name);
+ //Console.WriteLine("对象名称:" + TopType.Name);
//获取下级属性
TopType.TypeList = ReadModel(PerentType);
//}
@@ -91,19 +91,19 @@ namespace CoderEngine
///
public List ReadModel(Type PerentType)
{
- Console.WriteLine("对" + TopType.Name + "进行详细处理");
+ //Console.WriteLine("对" + TopType.Name + "进行详细处理");
//获取属性
PropertyInfo[] Plist = PerentType.GetProperties();
List TypeList = new List();
- Console.WriteLine("遍历"+ TopType.Name + "属性: 属性数量为"+ Plist.Count());
+ //Console.WriteLine("遍历"+ TopType.Name + "属性: 属性数量为"+ Plist.Count());
foreach (var m in Plist)
{
TypeMode TM = new TypeMode();
TM.Name = m.Name;
- Console.WriteLine("----" + TopType.Name + "下属性名:" + TM.Name);
+ //Console.WriteLine("----" + TopType.Name + "下属性名:" + TM.Name);
TM.MemberType = m.PropertyType;
- Console.WriteLine("----" + TopType.Name + "下属性" + TM.Name + " 类型为" + TM.MemberType.Name + "完整类型名" + TM.MemberType.Name);
+ //Console.WriteLine("----" + TopType.Name + "下属性" + TM.Name + " 类型为" + TM.MemberType.Name + "完整类型名" + TM.MemberType.Name);
//如果是List类型 则往下解析
if (TM.MemberType.Name == "List`1")
{
@@ -125,7 +125,7 @@ namespace CoderEngine
TM.DownParamType = SonType;
TM.DownParamName = SonType.Name;
- Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理");
+ //Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理");
TM.TypeList = ReadModel(SonType);
diff --git a/UseEngineConsolo/Program.cs b/UseEngineConsolo/Program.cs
index c95eb98..39054d0 100644
--- a/UseEngineConsolo/Program.cs
+++ b/UseEngineConsolo/Program.cs
@@ -13,6 +13,7 @@ namespace UseEngineConsolo
{
public int a { get; set; }
public int aa { get; set; }
+ public DateTime? date { get; set; }
public List Blist { get; set; }
}
@@ -40,12 +41,17 @@ namespace UseEngineConsolo
Console.WriteLine();
string JavaCode = new JavaCodeWrite().ModelToJavaCode("UseEngineConsolo", "A");
-
Console.WriteLine();
Console.WriteLine("C#实体类动态转Java代码结果:");
Console.WriteLine();
-
Console.WriteLine(JavaCode);
+ Console.WriteLine();
+ Console.WriteLine();
+ string CSCode = new CSharpCodeWrite().ModelToJavaCode("UseEngineConsolo", "A");
+ Console.WriteLine();
+ Console.WriteLine("C#实体类动态转C#客户端代码结果:");
+ Console.WriteLine();
+ Console.WriteLine(CSCode);
Console.ReadLine();
}
}