.Net新手通常容易把属性(Property)跟特性(Attribute)搞混,其实这是两种不同的东西
属性指的类中封装的数据字段;而特性是对类、字段、方法和属性等元素标注的声明性信息
如下代码(Id、Name为User的属性,[DbKey]为Id的特性)
/// <summary>
/// 用户信息
/// </summary>
public class User
{
[DbKey]
public string Id { get; set; }
public string Name { get; set; }
}
特性分预定义特性和自定义特性,本节主要讲述自定义特性
特性能解决什么问题?
假如现在需要通过定义一些实体类,动态创建出对应的数据库表,该怎么做呢?
直接上代码
namespace CustomerAttribute
{
/// <summary>
/// 数据库主键
/// </summary>
public class DbKey : Attribute
{
public string Description { get; set; }
public DbKey()
{
}
public DbKey(string description)
{
this.Description = description;
}
}
}
namespace CustomerAttribute
{
/// <summary>
/// 用户信息
/// </summary>
public class User
{
[DbKey]
public string Id { get; set; }
public string Name { get; set; }
}
/// <summary>
/// 用户角色
/// </summary>
public class UserRole
{
[DbKey("用户ID")]
public string UserId { get; set; }
[DbKey("角色ID")]
public string RoleId { get; set; }
}
}
namespace CustomerAttribute
{
class Program
{
/// <summary>
/// 获取数据库主键字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static IEnumerable<PropertyInfo> getDbKeyFields<T>()
{
// 获取当前类中的公共字段
var fields = typeof(T).GetProperties();
// 查找有DbKey特性的字段
var keyFields = fields.Where(field => (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey)) != null);
return keyFields;
}
private static string getDescription(PropertyInfo field)
{
string result = string.Empty;
var dbKey = (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey));
if (dbKey != null) result = dbKey.Description;
return result;
}
static void Main(string[] args)
{
try
{
var userKeyFields = getDbKeyFields<User>();
Console.WriteLine("User表的主键为:" + string.Join(",", userKeyFields.Select(field => field.Name)));
var userRoleKeyFields = getDbKeyFields<UserRole>();
Console.WriteLine("UserRole表的主键为:" + string.Join(",", userRoleKeyFields.Select(field => field.Name)));
foreach (PropertyInfo field in userRoleKeyFields)
{
string description = getDescription(field);
Console.WriteLine(string.Format("{0}字段的描述信息为:{1}", field.Name, description));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.ReadLine();
}
}
}
}










