ServParam2ClientModelEngine/CoderEngine/_ModleReader.cs

159 lines
5.3 KiB
C#
Raw Normal View History

2018-10-17 22:54:58 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引用反射
using System.Reflection;
namespace CoderEngine
{
2018-10-18 17:59:03 +08:00
public class A
{
public string UserID { get; set; }
public int Sex { get; set; }
public DateTime? Birthday { get; set; }
public List<B> SunList { get; set; }
}
public class B
{
public string SouUserID { get; set; }
public List<C> Girlfriends { get; set; }
}
public class C
{
public string GirlName { get; set; }
}
2018-10-17 22:54:58 +08:00
/// <summary>
/// 临时存储遍历出来的类型描述
/// </summary>
public class TypeMode
{
/// <summary>
/// 字段名称
/// </summary>
public Type MemberType { get; set; }
/// <summary>
/// 类型名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 是否有嵌套下级
/// </summary>
public int IsHaveSou = 0;
/// <summary>
/// 嵌套类型 如果有包裹下级类型的话 则不为空 Null
/// </summary>
public List<TypeMode> TypeList = null;
/// <summary>
/// 下级参数类型
/// </summary>
public Type DownParamType { get; set; }
/// <summary>
/// 下级参数名称
/// </summary>
public string DownParamName { get; set; }
}
/// <summary>
/// 模型读取
/// </summary>
2018-10-18 07:00:43 +08:00
public class _ModleReader
2018-10-17 22:54:58 +08:00
{
public TypeMode TopType = new TypeMode();
/// <summary>
/// 检查程序及下的实体类
/// </summary>
/// <param name="_AssemblyName">程序集名称</param>
/// <param name="_TypeName">ModelClass类型名称</param>
/// <returns></returns>
public TypeMode ModelCheck(string _AssemblyName, string _TypeName)
{
2018-10-18 07:00:43 +08:00
//Console.WriteLine("");
//Console.WriteLine("====== C# 数据模型转Java实体类代码实现 ======");
//Console.WriteLine("");
//Console.WriteLine("读取程序集" + _AssemblyName + "下实体类" + _TypeName + "");
2018-10-17 22:54:58 +08:00
//取得类型
Type PerentType = Assembly.Load(_AssemblyName).GetType(_AssemblyName + "." + _TypeName);
if (PerentType == null)
{
2018-10-18 07:00:43 +08:00
//Console.WriteLine("读取程序集或类不存在");
2018-10-17 22:54:58 +08:00
}
2018-10-18 07:00:43 +08:00
//Console.WriteLine("已成功反射获取程序集");
2018-10-17 22:54:58 +08:00
TopType.MemberType = PerentType;
2018-10-18 07:00:43 +08:00
//Console.WriteLine("对象类型:"+ TopType.MemberType);
2018-10-17 22:54:58 +08:00
TopType.Name = PerentType.Name;
2018-10-18 07:00:43 +08:00
//Console.WriteLine("对象名称:" + TopType.Name);
2018-10-17 22:54:58 +08:00
//获取下级属性
TopType.TypeList = ReadModel(PerentType);
//}
return TopType;
}
/// <summary>
/// 读取类型
/// </summary>
/// <param name="MethodItem"></param>
public List<TypeMode> ReadModel(Type PerentType)
{
2018-10-18 07:00:43 +08:00
//Console.WriteLine("对" + TopType.Name + "进行详细处理");
2018-10-17 22:54:58 +08:00
//获取属性
PropertyInfo[] Plist = PerentType.GetProperties();
List<TypeMode> TypeList = new List<TypeMode>();
2018-10-18 07:00:43 +08:00
//Console.WriteLine("遍历"+ TopType.Name + "属性: 属性数量为"+ Plist.Count());
2018-10-17 22:54:58 +08:00
foreach (var m in Plist)
{
TypeMode TM = new TypeMode();
TM.Name = m.Name;
2018-10-18 07:00:43 +08:00
//Console.WriteLine("----" + TopType.Name + "下属性名:" + TM.Name);
2018-10-17 22:54:58 +08:00
TM.MemberType = m.PropertyType;
2018-10-18 07:00:43 +08:00
//Console.WriteLine("----" + TopType.Name + "下属性" + TM.Name + " 类型为" + TM.MemberType.Name + "完整类型名" + TM.MemberType.Name);
2018-10-17 22:54:58 +08:00
//如果是List<T>类型 则往下解析
if (TM.MemberType.Name == "List`1")
{
//注意:这里需要考虑排除无限极自我嵌套实体类型 否则出现死循环
//暂未处理
//TM.MemberType.Name的FullName的值为 "System.Collections.Generic.List`1[[ --集合内嵌类型名称--, CoderEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"
//其中包含内嵌List的名称 我们要将其取出 得到的值例如 “程序集.*.*类型”
var SonParamName = TM.MemberType.FullName.Substring(TM.MemberType.FullName.IndexOf("[") + 2);
SonParamName = SonParamName.Substring(0, SonParamName.IndexOf(","));
//取出集合内子类型所属的程序集名字
string SonParamFromAssemblyName = SonParamName.Substring(0,SonParamName.IndexOf('.'));
//反射载入子类型的类型
Type SonType = Assembly.Load(SonParamFromAssemblyName).GetType(SonParamName);
TM.IsHaveSou = 1;
TM.DownParamType = SonType;
TM.DownParamName = SonType.Name;
2018-10-18 07:00:43 +08:00
//Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理");
2018-10-17 22:54:58 +08:00
TM.TypeList = ReadModel(SonType);
}
TypeList.Add(TM);
}
return TypeList;
}
}
}