最近移植之前写的几个类,发现特性操作发生了一些改变。

直接看代码,建立表和字段特性类,添加一个用户表,设置好特性。

  1. using System;
  2.  
  3. namespace TestDemo
  4. {
  5. /// <summary>
  6. /// 表实体特性
  7. /// </summary>
  8. [AttributeUsage(AttributeTargets.Class, Inherited = false)]
  9. public class TableAttribute : Attribute
  10. {
  11. /// <summary>
  12. /// 数据库表名称
  13. /// </summary>
  14. public string TableName { get; set; }
  15. /// <summary>
  16. /// 数据库表注释
  17. /// </summary>
  18. public string Description { get; set; }
  19.  
  20. public TableAttribute()
  21. {
  22. TableName = string.Empty;
  23. Description = string.Empty;
  24. }
  25. }
  26. }

表特性

  1. using System;
  2.  
  3. namespace TestDemo
  4. {
  5. /// <summary>
  6. /// 列特性
  7. /// </summary>
  8. [AttributeUsage( AttributeTargets.Property , AllowMultiple = false)]
  9. public class TableColumnAttribute : Attribute
  10. {
  11. /// <summary>
  12. /// 列名称
  13. /// </summary>
  14. public string ColumnName { get; set; }
  15. /// <summary>
  16. /// 字段说明
  17. /// </summary>
  18. public string Description { get; set; }
  19. /// <summary>
  20. /// 是否是主键
  21. /// </summary>
  22. public bool IsPrimaryKey { get; set; }
  23. /// <summary>
  24. /// 主键是否自动增长
  25. /// </summary>
  26. public bool IsPrimaryKeyAuto { get; set; }
  27. /// <summary>
  28. /// 数据库数据类型
  29. /// </summary>
  30. public string DbDataType { get; set; }
  31. /// <summary>
  32. /// 字符串最大长度
  33. /// </summary>
  34. public int MaxLength { get; set; }
  35. /// <summary>
  36. /// 是否不可为空
  37. /// </summary>
  38. public bool NotNull { get; set; }
  39.  
  40. public TableColumnAttribute()
  41. {
  42. ColumnName = string.Empty;
  43. Description = string.Empty;
  44. IsPrimaryKey = false;
  45. IsPrimaryKeyAuto = false;
  46. DbDataType = "varchar";
  47. MaxLength = ;
  48. NotNull = false;
  49. }
  50. }
  51. }

表字段特性

  1. namespace TestDemo
  2. {
  3. [Table(Description = "用户表", TableName = "USER")]
  4. public class User
  5. {
  6. [TableColumn(ColumnName = "ID", DbDataType = "int", Description = "主键", IsPrimaryKey = true, IsPrimaryKeyAuto = true, MaxLength = , NotNull = true)]
  7. public int Id { get; set; }
  8.  
  9. [TableColumn(ColumnName = "LOGINNO", DbDataType = "varchar", Description = "登录名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = true)]
  10. public string LoginNo { get; set; }
  11.  
  12. [TableColumn(ColumnName = "USERNAME", DbDataType = "varchar", Description = "用户姓名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
  13. public string UserName { get; set; }
  14.  
  15. [TableColumn(ColumnName = "NICKNAME", DbDataType = "varchar", Description = "昵称", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
  16. public string NickName { get; set; }
  17.  
  18. [TableColumn(ColumnName = "TEL", DbDataType = "varchar", Description = "电话", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
  19. public string Tel { get; set; }
  20. }
  21. }

用户表

获取用户表以及表字段的特性。

  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4.  
  5. namespace TestDemo
  6. {
  7. public class Program
  8. {
  9. public static void Main(string[] args)
  10. {
  11. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  12.  
  13. TableAttribute table = GetTableAttribute<User>();
  14. Console.WriteLine("User(TableName:" + table.TableName + "\r\n,Description:" + table.Description + ")");
  15. Console.WriteLine();
  16.  
  17. TableColumnAttribute colId = GetTableColumnAttribute<User>("Id");
  18. Console.WriteLine("Id(ColumnName:" + colId.ColumnName
  19. + "\r\n,DbDataType:" + colId.DbDataType
  20. + "\r\n,Description:" + colId.Description
  21. + "\r\n,IsPrimaryKey:" + colId.IsPrimaryKey
  22. + "\r\n,IsPrimaryKeyAuto:" + colId.IsPrimaryKeyAuto
  23. + "\r\n,MaxLength:" + colId.MaxLength
  24. + "\r\n,NotNull:" + colId.NotNull + ")");
  25. Console.WriteLine();
  26.  
  27. TableColumnAttribute colName = GetTableColumnAttribute<User>("UserName");
  28. Console.WriteLine("UserName(ColumnName:" + colName.ColumnName
  29. + "\r\n,DbDataType:" + colName.DbDataType
  30. + "\r\n,Description:" + colName.Description
  31. + "\r\n,IsPrimaryKey:" + colName.IsPrimaryKey
  32. + "\r\n,IsPrimaryKeyAuto:" + colName.IsPrimaryKeyAuto
  33. + "\r\n,MaxLength:" + colName.MaxLength
  34. + "\r\n,NotNull:" + colName.NotNull + ")");
  35.  
  36. Console.ReadLine();
  37. }
  38.  
  39. /// <summary>
  40. /// 获取表特性
  41. /// </summary>
  42. /// <typeparam name="T"></typeparam>
  43. /// <returns></returns>
  44. public static TableAttribute GetTableAttribute<T>()
  45. {
  46. Type t = typeof(T);
  47. TableAttribute m = t.GetTypeInfo().GetCustomAttribute<TableAttribute>();
  48. return m;
  49. }
  50.  
  51. /// <summary>
  52. /// 获取列特性
  53. /// </summary>
  54. /// <typeparam name="T"></typeparam>
  55. /// <param name="propertyName"></param>
  56. /// <returns></returns>
  57. public static TableColumnAttribute GetTableColumnAttribute<T>(string propertyName)
  58. {
  59. TableColumnAttribute m = null;
  60.  
  61. Type t = typeof(T);
  62. PropertyInfo[] arryProperty = t.GetProperties();
  63. if (arryProperty != null)
  64. {
  65. foreach (PropertyInfo p in arryProperty)
  66. {
  67. if (p.Name == propertyName)
  68. {
  69. m = p.GetCustomAttribute<TableColumnAttribute>();
  70. }
  71. }
  72. }
  73.  
  74. return m;
  75. }
  76. }
  77. }

Program

运行起来看看获取的情况!

.net core自定义特性操作的更多相关文章

  1. Asp.net core通过自定义特性实现双端数据验证的一些想法

    asp.net core集成了非常方便的数据绑定和数据校验机制,配合操作各种easy的vs,效率直接高到飞起. 通过自定义验证特性(Custom Validation Attribute)可以实现对于 ...

  2. C#反射与特性(七):自定义特性以及应用

    目录 1,属性字段的赋值和读值 2,自定义特性和特性查找 2.1 特性规范和自定义特性 2.2 检索特性 3,设计一个数据验证工具 3.1 定义抽象验证特性类 3.2 实现多个自定义验证特性 3.3 ...

  3. Asp.net core自定义依赖注入容器,替换自带容器

    依赖注入 在asp.net core程序中,众所周知,依赖注入基本上贯穿了整个项目,以通用的结构来讲解,控制器层(Controller层)依赖业务层(Service层),业务层依赖于仓储层(Repos ...

  4. C#自定义特性实例

    元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1.什么是特性   (1)属性与特性的区别  属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段, ...

  5. Shader的自定义特性使用

    使用自定义特性关键字,可以动态对Shader某一部分代码进行开关操作 shader(定义了KEYWORD1特性): 定义:#pragma shader_feature KEYWORD1 判断:#ifd ...

  6. c#通过反射获取类上的自定义特性

    c#通过反射获取类上的自定义特性 本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html 下面这个 ...

  7. .Net 特性 attribute 学习 ----自定义特性

    什么是特性? [Obsolete("不要用无参构造函数",true)] 放在方式上, 该方法就不能使用了  [Serializable]放在类上面.该类就是可以序列化和反序列化使用 ...

  8. 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版

    代码走查25条疑问   代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...

  9. C# 反射通过GetCustomAttributes方法,获得自定义特性

    http://blog.csdn.net/litao2/article/details/17633107 使用反射访问: 自定义属性的信息和对其进行操作的方法. 一.实例1 1.代码: 如:Syste ...

随机推荐

  1. pip安装flask问题解决

    环境:python 2.7 pip install virtualenv pip install flask 提示成功但无效 查看http://docs.jinkan.org/docs/flask/i ...

  2. leetcode 181 Employees Earning More Than Their Managers 不会分析的数据库复杂度

    https://leetcode.com/problems/employees-earning-more-than-their-managers/description/ 老师上课没分析这些的复杂度, ...

  3. netbeans 窗体字体大小设置

    当计算机分辨率变大的时候,打开netbeans的时候,字体就会变得越来越小 看起来很不爽,所要就要改变一下,窗体字体大小. 在网上找到了一段修改netbeans窗体字体大小的配置信息,现标记起来,以便 ...

  4. mac os 和 ubuntu 上测试工具check-0.9.10的安装

    由于工作需要,要使用check 这个单元测试工具. 首先,说一说在Mac10.9上面的安装.我是直接在官网(http://check.sourceforge.net)上下载源码包. 1,解压 2,进入 ...

  5. 《Flink 源码解析》—— 源码编译运行

    更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...

  6. 工作采坑札记:1. Hadoop中的BytesWritable误区

    1. 背景 近日帮外部门的同事处理一个小需求,就是将HDFS中2018年至今所有存储的sequence序列化文件读取出来,重新保存成文本格式,以便于他后续进行处理.由于同事主要做机器学习方向,对had ...

  7. VCL

    vcl常用配置 不缓存摸一个资源 在vcl_recv中 if (req.url ~ "private") { return (pass); } 动静分离 先定一个多个backend ...

  8. springboot 整合redisson

    整合代码已经过测试 1.pom <!-- redisson --> <dependency> <groupId>org.redisson</groupId&g ...

  9. Hibernate课程 初探多对多映射1-1 多对多应用场景

    1 用途: 员工和项目之间的多对多关系 2 实现: 员工表和项目表之外,建立员工和项目关联表来实现: 3 hibernate应用: set元素和many-to-many来实现

  10. css 引入方式以及css的选择器

    一.css的引入方式: 1.行内样式 <div> <p style="color: red">我是一个段落</p> </div> 2 ...