123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using FCS.Models;
- namespace FCS.Common
- {
- /// <summary>
- /// 获取实体类Attribute自定义属性
- /// </summary>
- public sealed class EntityAttributeHelper
- {
- /// <summary>
- /// 获取实体对象表名
- /// </summary>
- /// <returns></returns>
- public static string GetEntityTable<T>()
- {
- Type objTye = typeof(T);
- string entityName = "";
- var tableAttribute = objTye.GetCustomAttributes(true).OfType<TableAttribute>();
- var descriptionAttributes = tableAttribute as TableAttribute[] ?? tableAttribute.ToArray();
- entityName = descriptionAttributes.Any() ? descriptionAttributes.ToList()[0].Name : objTye.Name;
- return entityName;
- }
- /// <summary>
- /// 获取实体类 字段中文名称
- /// </summary>
- /// <param name="pi">字段属性信息</param>
- /// <returns></returns>
- public static string GetFieldText(PropertyInfo pi)
- {
- string txt = "";
- var descAttrs = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true);
- if (descAttrs.Any())
- {
- DisplayNameAttribute descAttr = descAttrs[0] as DisplayNameAttribute;
- if (descAttr != null) txt = descAttr.DisplayName;
- }
- else
- {
- txt = pi.Name;
- }
- return txt;
- }
- /// <summary>
- /// 获取实体类中文名称
- /// </summary>
- /// <returns></returns>
- public static string GetClassName<T>()
- {
- Type objTye = typeof(T);
- string entityName = "";
- var busingessNames = objTye.GetCustomAttributes(true).OfType<DisplayNameAttribute>();
- var descriptionAttributes = busingessNames as DisplayNameAttribute[] ?? busingessNames.ToArray();
- if (descriptionAttributes.Any())
- {
- entityName = descriptionAttributes.ToList()[0].DisplayName;
- }
- else
- {
- entityName = objTye.Name;
- }
- return entityName;
- }
- /// <summary>
- /// 获取访问元素
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- public static PropertyInfo[] GetProperties(Type type)
- {
- return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- }
- }
- }
|