/// <summary>
/// 数据库表名
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class DBTableNameAttribute : Attribute
{
public string Name { get; set; }
public DBTableNameAttribute(string Name)
{
this.Name = Name;
}
}
/// <summary>
/// 主键名
/// </summary>
[AttributeUsage(AttributeTargets.Class )]
public class DBTableFiledPrimaryKeyAttribute : Attribute
{
public string Name { get; set; }
public DBTableFiledPrimaryKeyAttribute(string Name)
{
this.Name = Name;
}
}
/// <summary>
/// 字段名
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class DBTableFiledNameAttribute : Attribute
{
public string Name { get; set; }
public DBTableFiledNameAttribute(string Name)
{
this.Name = Name;
}
}
/// <summary>
/// 唯一值
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class DBTableFiledUniqueAttribute : Attribute
{
public DBTableFiledUniqueAttribute( )
{
}
}
/// <summary>
/// 对MemberInfo类扩展方法
/// </summary>
public static class CustomMemberInfo
{
/// <summary>
/// 判断是否存在相应的特性
/// </summary>
/// <typeparam name="T">特性类</typeparam>
/// <param name="type"></param>
/// <returns></returns>
static public bool HasAttribute<T>(this System.Reflection.MemberInfo type) where T : class
{
object[] attributes = type.GetCustomAttributes(false);
foreach (Attribute attr in attributes)
{
//判断Attribute 中是否 为 UniqueColumnAttribute
if (attr is T)
{
return true;
}
}
return false;
}
/// <summary>
/// 获取相应的Attribute对象 如 var attr=typeof(Person).GetAttribute<DBTableNameAttribute>();
/// </summary>
/// <typeparam name="T">Attribute类</typeparam>
/// <param name="type">实体类</param>
/// <returns>Attribute对象</returns>
static public T GetAttribute<T>(this System.Reflection.MemberInfo type) where T : class
{
Attribute classAttribute = Attribute.GetCustomAttribute(type, typeof(T));
return classAttribute as T;
}
}