public static class AttributeHelper
{
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;
}
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;
}
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;
}
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;
}
public static bool IsPrimary(this Type type, PropertyInfo property)
{
string primaryKeyName = type.GetPrimary();
string columnName = property.GetColName();
return (primaryKeyName == columnName);
}
}