You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB
C#

using System.ComponentModel;
using System.Reflection;
namespace DS.Module.Core.Extensions;
/// <summary>
/// 实体拓展方法
/// </summary>
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<EntityFieldProperty> GetEntityFieldByName(this string entityName)
{
var res = new List<EntityFieldProperty>();
// 获取当前程序集
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;
}
/// <summary>
/// 获取实体字段信息
/// </summary>
/// <param name="t"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<EntityFieldProperty> GetEntityFieldProperty<T>(this T t)
{
var res = new List<EntityFieldProperty>();
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
{
/// <summary>
/// 实体名称
/// </summary>
[Description("实体名称")]
public string EntityName { get; set; }
/// <summary>
/// 字段名称
/// </summary>
[Description("字段名称")]
public string FieldName { get; set; }
/// <summary>
/// 字段描述
/// </summary>
[Description("字段描述")]
public string FieldComment { get; set; }
/// <summary>
/// 字段类型
/// </summary>
[Description("字段类型")]
public string FieldType { get; set; }
}