(更新时间)2021年5月19日 仓库温控系统(Winform) 04 获取特性值的扩展方法AttributeHelper

编程入门 行业动态 更新时间:2024-10-04 13:26:53

(<a href=https://www.elefans.com/category/jswz/34/1764312.html style=更新时间)2021年5月19日 仓库温控系统(Winform) 04 获取特性值的扩展方法AttributeHelper"/>

(更新时间)2021年5月19日 仓库温控系统(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);}
}

更多推荐

(更新时间)2021年5月19日 仓库温控系统(Winform) 04 获取特性值的扩展方法AttributeHelper

本文发布于:2024-02-14 16:05:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1764326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更新时间   仓库   特性   方法   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!