C# 自定义特性

Id字段上的DbKey就是自定义特性

/// <summary>
/// 用户信息
/// </summary>
public class User
{
    [DbKey]
    public string Id { get; set; }
    public string Name { get; set; }
}

继承Attribute,实现自定义特性DbKey

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();
            }
        }
    }
}
View Code

一些Orm的实现,就是通过解析特性信息,动态生成数据库表

转载于:https://www.cnblogs.com/jh007/p/6124179.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值