using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using FCS.Models;
namespace FCS.Common
{
///
/// 获取实体类Attribute自定义属性
///
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;
}
///
/// 获取实体类 字段中文名称
///
/// 字段属性信息
///
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;
}
///
/// 获取实体类中文名称
///
///
public static string GetClassName()
{
Type objTye = typeof(T);
string entityName = "";
var busingessNames = objTye.GetCustomAttributes(true).OfType();
var descriptionAttributes = busingessNames as DisplayNameAttribute[] ?? busingessNames.ToArray();
if (descriptionAttributes.Any())
{
entityName = descriptionAttributes.ToList()[0].DisplayName;
}
else
{
entityName = objTye.Name;
}
return entityName;
}
///
/// 获取访问元素
///
///
///
public static PropertyInfo[] GetProperties(Type type)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
}
}