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.

215 lines
6.8 KiB
C#

1 year ago
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;
}
/// <summary>
/// 获取实体有值内容
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static Dictionary<string, string> GetProperties<T>(this T t)
{
Dictionary<string, string> ret = new Dictionary<string, string>();
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;
}
/// <summary>
/// 获取实体有值内容
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string[] GetPropertiesArray<T>(this T t)
{
List<string> ret = new List<string>();
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();
}
1 year ago
}
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; }
}