using System; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Reflection; namespace Common { public sealed class EntityAttributeHelper { /// /// 获取实体对象表名 /// /// public static string GetEntityTable() { Type objTye = typeof(T); string entityName = ""; var tableAttribute = objTye.GetCustomAttributes(true).OfType(); var descriptionAttributes = tableAttribute as TableAttribute[] ?? tableAttribute.ToArray(); entityName = descriptionAttributes.Any() ? descriptionAttributes.ToList()[0].Name : objTye.Name; return entityName; } /// /// 获取实体对象Key /// /// public static string GetEntityKey() { Type objTye = typeof(T); string entityName = ""; var tableAttribute = objTye.GetCustomAttributes(true).OfType(); var descriptionAttributes = tableAttribute as Key[] ?? tableAttribute.ToArray(); entityName = descriptionAttributes.Any() ? descriptionAttributes.ToList()[0].Name : objTye.Name; return entityName; } /// /// 获取ApiAction值 /// /// public static string GetMark() { Type objTye = typeof(T); string entityName = ""; var tableAttribute = objTye.GetCustomAttributes(true).OfType(); var descriptionAttributes = tableAttribute as MarkAttribute[] ?? tableAttribute.ToArray(); entityName = descriptionAttributes.Any() ? descriptionAttributes.ToList()[0].Name : objTye.Name; return entityName; } /// /// 获取访问元素 /// /// /// public static PropertyInfo[] GetProperties(Type type) { return type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } /// /// 获取实体对象表名 /// /// public static string GetCalssTable() { Type objTye = typeof(T); if (objTye.Name.Contains("List")) { objTye = objTye.GenericTypeArguments[0]; } string entityName = ""; try { var tableAttribute = objTye.GetCustomAttributes(true).OfType(); var descriptionAttributes = tableAttribute as TableAttribute[] ?? tableAttribute.ToArray(); entityName = descriptionAttributes.Any() ? descriptionAttributes.ToList()[0].Name : objTye.Name; } catch (Exception e) { } return entityName+"_"; } } }