using System.ComponentModel; using System.Reflection; namespace DS.Module.Core.Extensions; /// /// 实体拓展方法 /// public static class EntityExtensions { public static Type GetEntity(this string entityName) { // 获取当前程序集 Assembly assembly = Assembly.Load("DS.WMS.Core"); // 获取所有类型 Type[] types = assembly.GetTypes(); var t = types.Where(a => a.Name == entityName).FirstOrDefault(); // Type type = null; // if (t != null) // { // type = Type.GetType(t.FullName, true, true); // } var temp = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).GetValue(0); return t; } public static List GetEntityFieldByName(this string entityName) { var res = new List(); // 获取当前程序集 Assembly assembly = Assembly.Load("DS.WMS.Core"); // 获取所有类型 Type[] types = assembly.GetTypes(); var t = types.Where(a => a.Name == entityName).FirstOrDefault(); PropertyInfo[] properties = t.GetProperties(); var entiyName = t.Name; if (properties.Length <= 0) { return res; } foreach (PropertyInfo item in properties) { string name = item.Name; //名称 // object value = item.GetValue(t, null); //值 string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 属性值 if (string.IsNullOrEmpty(des)) des = name; if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { var typeName = item.PropertyType.Name; if (Nullable.GetUnderlyingType(item.PropertyType) != null) { typeName = Nullable.GetUnderlyingType(item.PropertyType).Name; } res.Add(new EntityFieldProperty { FieldName = name, EntityName = entiyName, FieldComment = des, FieldType = typeName } ); } } return res; // var temp = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).GetValue(0); // return t; } /// /// 获取实体字段信息 /// /// /// /// public static List GetEntityFieldProperty(this T t) { var res = new List(); if (t == null) { return res; } PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); var entiyName = t.GetType().Name; if (properties.Length <= 0) { return res; } foreach (PropertyInfo item in properties) { string name = item.Name; //名称 object value = item.GetValue(t, null); //值 string des = ((DescriptionAttribute)Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute))).Description;// 属性值 if (string.IsNullOrEmpty(des)) des = name; if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { var typeName = item.PropertyType.Name; if (Nullable.GetUnderlyingType(item.PropertyType) != null) { typeName = Nullable.GetUnderlyingType(item.PropertyType).Name; } res.Add(new EntityFieldProperty { FieldName = name, EntityName = entiyName, FieldComment = des, FieldType = typeName } ); } } return res; } } public class EntityFieldProperty { /// /// 实体名称 /// [Description("实体名称")] public string EntityName { get; set; } /// /// 字段名称 /// [Description("字段名称")] public string FieldName { get; set; } /// /// 字段描述 /// [Description("字段描述")] public string FieldComment { get; set; } /// /// 字段类型 /// [Description("字段类型")] public string FieldType { get; set; } }