2021-05-21 仓库温控系统(Winform) 04 获取特性值的扩展方法AttributeHelper

public static class AttributeHelper
{
    /// <summary>
    /// 获取映射表名
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static string GetTName(this Type type)
    {
        string tableName = string.Empty;
        //获取指定类型的自定义特性
        object[] attrs = type.GetCustomAttributes(false);
        foreach (var  attr in attrs)
        {
            if(attr is TableAttribute)
            {
                TableAttribute tableAttribute = attr as TableAttribute;
                tableName = tableAttribute.TableName;
            }
        }
        if(string.IsNullOrEmpty(tableName))
        {
            tableName = type.Name;
        }
        return tableName;
    }

    /// <summary>
    /// 获取映射列名
    /// </summary>
    /// <param name="property"></param>
    /// <returns></returns>
    public static string GetColName(this PropertyInfo property)
    {
        string columnName = string.Empty;
        //获取指定类型的自定义特性
        object[] attrs = property.GetCustomAttributes(false);
        foreach (var attr in attrs)
        {
            if(attr is ColumnAttribute)
            {
                ColumnAttribute colAttr = attr as ColumnAttribute;
                columnName = colAttr.ColumnName;
            }
        }
        if(string.IsNullOrEmpty(columnName))
        {
            columnName = property.Name;
        }
        return columnName;
    }

    /// <summary>
    /// 判断主键是否自增
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static bool IsIncrement(this Type type)
    {
        //获取指定类型的自定义特性
        object[] attributes = type.GetCustomAttributes(false);
        foreach (var attr in attributes)
        {
            if (attr is PrimaryKeyAttribute)
            {
                PrimaryKeyAttribute primaryKeyAttr = attr as PrimaryKeyAttribute;
                return primaryKeyAttr.autoIncrement;
            }
        }
        return false;
    } 

    ///<summary>  
    /// 获取主键名  
    /// </summary>  
    /// <param name="type"></param>  
    /// <returns></returns>  
    public static string GetPrimary(this Type type)
    {
        object[] attributes = type.GetCustomAttributes(false);
        foreach (var attr in attributes)
        {
            if (attr is PrimaryKeyAttribute)
            {
                PrimaryKeyAttribute primaryKeyAttr = attr as PrimaryKeyAttribute;
                return primaryKeyAttr.PrimaryKey;
            }
        }
        return null;
    }

    /// <summary>  
    /// 判断属性是否为主键  
    /// </summary>  
    /// <param name="type"></param>  
    /// <param name="property"></param>  
    /// <returns></returns>  
    public static bool IsPrimary(this Type type, PropertyInfo property)
    {
        //获取主键名
        string primaryKeyName = type.GetPrimary();
        //获取指定属性的映射列名
        string columnName = property.GetColName();
        //判断是否相等
        return (primaryKeyName == columnName);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值