手把手教你实现自己的abp代码生成器 (2)

新建类MetaTableInfo

public class MetaTableInfo { /// <summary> /// 类的注释 /// </summary> public string ClassAnnotation { get; set; } /// <summary> /// 属性名称 /// </summary> public string Name { get; set; } /// <summary> /// 属性类型 /// </summary> public string PropertyType { get; set; } /// <summary> /// 属性注释 /// </summary> public string Annotation { get; set; } /// <summary> /// 根据类名 反射得到类的信息 /// </summary> /// <param>类名</param> /// <returns></returns> public static List<MetaTableInfo> GetMetaTableInfoListForAssembly(string className) { var list = new List<MetaTableInfo>(); //读取的程序集路径 需换成自己项目的下的程序集路径 string sourceAssemblyPath = "D:\\Project\\ZhouDaFu.XinYunFen.Core\\bin\\Debug\\netcoreapp2.0\\ZhouDaFu.XinYunFen.Core.dll"; Type[] types = Assembly.LoadFrom(sourceAssemblyPath).GetTypes(); foreach (var type in types) { if (type.Name.Equals(className)) { var classAnnotation = string.Empty; try { //获取类的注释 XmlElement xmlFromType = DocsByReflection.XMLFromType(type.GetTypeInfo()); classAnnotation = xmlFromType["summary"].InnerText.Trim(); } catch { } foreach (PropertyInfo properties in type.GetProperties()) { var metaTableInfo = new MetaTableInfo(); try { XmlElement documentation = DocsByReflection.XMLFromMember(type.GetProperty(properties.Name)); metaTableInfo.Annotation = documentation["summary"].InnerText.Trim(); metaTableInfo.ClassAnnotation = classAnnotation; } catch { metaTableInfo.Annotation = ""; } metaTableInfo.Name = properties.Name; if (properties.PropertyType == typeof(int)) { metaTableInfo.PropertyType = "int"; } else if (properties.PropertyType == typeof(int?)) { metaTableInfo.PropertyType = "int?"; } else if (properties.PropertyType == typeof(long)) { metaTableInfo.PropertyType = "long"; } else if (properties.PropertyType == typeof(long?)) { metaTableInfo.PropertyType = "long?"; } else if (properties.PropertyType == typeof(DateTime?)) { metaTableInfo.PropertyType = "DateTime?"; } else if (properties.PropertyType == typeof(decimal)) { metaTableInfo.PropertyType = "decimal"; } else if (properties.PropertyType == typeof(decimal?)) { metaTableInfo.PropertyType = "decimal?"; } else if (properties.PropertyType == typeof(string)) { metaTableInfo.PropertyType = "string"; } else if (properties.PropertyType == typeof(bool)) { metaTableInfo.PropertyType = "bool"; } else { metaTableInfo.PropertyType = properties.PropertyType.ToString().Split('.').Last().Replace("]", ""); } list.Add(metaTableInfo); } } } return list; } }

在Program类中添加如下代码

//反射程序集的方式生成相应代码 string className = "User";//跟类名保持一致 var metaTableInfoList = MetaTableInfo.GetMetaTableInfoListForAssembly(className);

启动控制台,获得User相关信息,报如下错误

1531114232956

这是缺少ABP相关程序集引用,使用NuGet安装如下程序集

1531114396512

启动控制台得到如下结果,剩下的就是字符串拼接了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpspds.html