This commit is contained in:
皓月 2018-10-18 07:00:43 +08:00
parent c729f2401f
commit dd54964321
8 changed files with 166 additions and 19 deletions

View File

@ -26,7 +26,7 @@ namespace CoderEngine
public string ModelToJavaCode(string _AssemblyName, string _TypeName) public string ModelToJavaCode(string _AssemblyName, string _TypeName)
{ {
TypeMode tm = new ModleReader().ModelCheck(_AssemblyName, _TypeName); TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm); ModelToJavaCodeWriter(tm);
return CodeStr; return CodeStr;
} }

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CoderEngine
{
public class CSharpCodeWrite
{
private string CodeStr = "";
/// <summary>
/// 一个代码缩进
/// </summary>
private string tabstr = " ";
/// <summary>
/// 逐行添加代码
/// </summary>
/// <param name="codeline"></param>
/// <returns></returns>
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);
}
}
}
}
/// <summary>
/// 类型处理
/// </summary>
/// <returns></returns>
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;
}
}
}

View File

@ -40,9 +40,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AjaxCodeWrite.cs" /> <Compile Include="AjaxCodeWrite.cs" />
<Compile Include="CSharpCodeWrite.cs" />
<Compile Include="GolangCodeWrite.cs" />
<Compile Include="JavaCodeWrite.cs" /> <Compile Include="JavaCodeWrite.cs" />
<Compile Include="ModleReader.cs" /> <Compile Include="_ModleReader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SwiftCodeWrite.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CoderEngine
{
class GolangCodeWrite
{
}
}

View File

@ -26,7 +26,7 @@ namespace CoderEngine
public string ModelToJavaCode(string _AssemblyName, string _TypeName) public string ModelToJavaCode(string _AssemblyName, string _TypeName)
{ {
TypeMode tm = new ModleReader().ModelCheck(_AssemblyName, _TypeName); TypeMode tm = new _ModleReader().ModelCheck(_AssemblyName, _TypeName);
ModelToJavaCodeWriter(tm); ModelToJavaCodeWriter(tm);
return CodeStr; return CodeStr;
} }
@ -102,6 +102,12 @@ namespace CoderEngine
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(",")); SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
return "List<" + SonParamName.Substring(SonParamName.LastIndexOf('.')+1) + ">"; 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 else
return "string"; return "string";
} }

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CoderEngine
{
class SwiftCodeWrite
{
}
}

View File

@ -44,7 +44,7 @@ namespace CoderEngine
/// <summary> /// <summary>
/// 模型读取 /// 模型读取
/// </summary> /// </summary>
public class ModleReader public class _ModleReader
{ {
public TypeMode TopType = new TypeMode(); public TypeMode TopType = new TypeMode();
@ -59,25 +59,25 @@ namespace CoderEngine
/// <returns></returns> /// <returns></returns>
public TypeMode ModelCheck(string _AssemblyName, string _TypeName) public TypeMode ModelCheck(string _AssemblyName, string _TypeName)
{ {
Console.WriteLine(""); //Console.WriteLine("");
Console.WriteLine("====== C# 数据模型转Java实体类代码实现 ======"); //Console.WriteLine("====== C# 数据模型转Java实体类代码实现 ======");
Console.WriteLine(""); //Console.WriteLine("");
Console.WriteLine("读取程序集" + _AssemblyName + "下实体类" + _TypeName + ""); //Console.WriteLine("读取程序集" + _AssemblyName + "下实体类" + _TypeName + "");
//取得类型 //取得类型
Type PerentType = Assembly.Load(_AssemblyName).GetType(_AssemblyName + "." + _TypeName); Type PerentType = Assembly.Load(_AssemblyName).GetType(_AssemblyName + "." + _TypeName);
if (PerentType == null) if (PerentType == null)
{ {
Console.WriteLine("读取程序集或类不存在"); //Console.WriteLine("读取程序集或类不存在");
} }
Console.WriteLine("已成功反射获取程序集"); //Console.WriteLine("已成功反射获取程序集");
TopType.MemberType = PerentType; TopType.MemberType = PerentType;
Console.WriteLine("对象类型:"+ TopType.MemberType); //Console.WriteLine("对象类型:"+ TopType.MemberType);
TopType.Name = PerentType.Name; TopType.Name = PerentType.Name;
Console.WriteLine("对象名称:" + TopType.Name); //Console.WriteLine("对象名称:" + TopType.Name);
//获取下级属性 //获取下级属性
TopType.TypeList = ReadModel(PerentType); TopType.TypeList = ReadModel(PerentType);
//} //}
@ -91,19 +91,19 @@ namespace CoderEngine
/// <param name="MethodItem"></param> /// <param name="MethodItem"></param>
public List<TypeMode> ReadModel(Type PerentType) public List<TypeMode> ReadModel(Type PerentType)
{ {
Console.WriteLine("对" + TopType.Name + "进行详细处理"); //Console.WriteLine("对" + TopType.Name + "进行详细处理");
//获取属性 //获取属性
PropertyInfo[] Plist = PerentType.GetProperties(); PropertyInfo[] Plist = PerentType.GetProperties();
List<TypeMode> TypeList = new List<TypeMode>(); List<TypeMode> TypeList = new List<TypeMode>();
Console.WriteLine("遍历"+ TopType.Name + "属性: 属性数量为"+ Plist.Count()); //Console.WriteLine("遍历"+ TopType.Name + "属性: 属性数量为"+ Plist.Count());
foreach (var m in Plist) foreach (var m in Plist)
{ {
TypeMode TM = new TypeMode(); TypeMode TM = new TypeMode();
TM.Name = m.Name; TM.Name = m.Name;
Console.WriteLine("----" + TopType.Name + "下属性名:" + TM.Name); //Console.WriteLine("----" + TopType.Name + "下属性名:" + TM.Name);
TM.MemberType = m.PropertyType; 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<T>类型 则往下解析 //如果是List<T>类型 则往下解析
if (TM.MemberType.Name == "List`1") if (TM.MemberType.Name == "List`1")
{ {
@ -125,7 +125,7 @@ namespace CoderEngine
TM.DownParamType = SonType; TM.DownParamType = SonType;
TM.DownParamName = SonType.Name; TM.DownParamName = SonType.Name;
Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理"); //Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理");
TM.TypeList = ReadModel(SonType); TM.TypeList = ReadModel(SonType);

View File

@ -13,6 +13,7 @@ namespace UseEngineConsolo
{ {
public int a { get; set; } public int a { get; set; }
public int aa { get; set; } public int aa { get; set; }
public DateTime? date { get; set; }
public List<B> Blist { get; set; } public List<B> Blist { get; set; }
} }
@ -40,12 +41,17 @@ namespace UseEngineConsolo
Console.WriteLine(); Console.WriteLine();
string JavaCode = new JavaCodeWrite().ModelToJavaCode("UseEngineConsolo", "A"); string JavaCode = new JavaCodeWrite().ModelToJavaCode("UseEngineConsolo", "A");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("C#实体类动态转Java代码结果"); Console.WriteLine("C#实体类动态转Java代码结果");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine(JavaCode); 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(); Console.ReadLine();
} }
} }