using System; using System.Collections.Generic; using System.Linq; using System.Text; //引用反射 using System.Reflection; namespace CoderEngine { public class A { public string UserID { get; set; } public int Sex { get; set; } public DateTime? Birthday { get; set; } public List SunList { get; set; } } public class B { public string SouUserID { get; set; } public List Girlfriends { get; set; } } public class C { public string GirlName { get; set; } } /// /// 临时存储遍历出来的类型描述 /// public class TypeMode { /// /// 字段名称 /// public Type MemberType { get; set; } /// /// 类型名称 /// public string Name { get; set; } /// /// 是否有嵌套下级 /// public int IsHaveSou = 0; /// /// 嵌套类型 如果有包裹下级类型的话 则不为空 Null /// public List TypeList = null; /// /// 下级参数类型 /// public Type DownParamType { get; set; } /// /// 下级参数名称 /// public string DownParamName { get; set; } } /// /// 模型读取 /// public class _ModleReader { public TypeMode TopType = new TypeMode(); /// /// 检查程序及下的实体类 /// /// 程序集名称 /// ModelClass类型名称 /// public TypeMode ModelCheck(string _AssemblyName, string _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("已成功反射获取程序集"); TopType.MemberType = PerentType; //Console.WriteLine("对象类型:"+ TopType.MemberType); TopType.Name = PerentType.Name; //Console.WriteLine("对象名称:" + TopType.Name); //获取下级属性 TopType.TypeList = ReadModel(PerentType); //} return TopType; } /// /// 读取类型 /// /// public List ReadModel(Type PerentType) { //Console.WriteLine("对" + TopType.Name + "进行详细处理"); //获取属性 PropertyInfo[] Plist = PerentType.GetProperties(); List TypeList = new List(); //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); TM.MemberType = m.PropertyType; //Console.WriteLine("----" + TopType.Name + "下属性" + TM.Name + " 类型为" + TM.MemberType.Name + "完整类型名" + TM.MemberType.Name); //如果是List类型 则往下解析 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; //Console.WriteLine("----对" + TopType.Name + "下属性" + TM.Name + " 嵌套泛型进行处理"); TM.TypeList = ReadModel(SonType); } TypeList.Add(TM); } return TypeList; } } }