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 static Dictionary GetProperties(this T t) { Dictionary ret = new Dictionary(); if (t == null) { return null; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return null; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; //实体类字段名称 string value = item.GetValue(t, null).ToString(); //该字段的值 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { ret.Add(name, value); //在此可转换value的类型 } } return ret; } /// /// 获取实体有值内容 /// /// /// /// public static string[] GetPropertiesArray(this T t) { List ret = new List(); if (t == null) { return null; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return null; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; //实体类字段名称 object value = item.GetValue(t, null); //该字段的值 if ((item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && value.IsNotNull()) { ret.Add(name); //在此可转换value的类型 } } return ret.ToArray(); } } 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; } }