.net core自定义特性操作
最近移植之前写的几个类,发现特性操作发生了一些改变。
直接看代码,建立表和字段特性类,添加一个用户表,设置好特性。
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 = 50; 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 = 0, NotNull = true)] 7 public int Id { get; set; } 8 9 [TableColumn(ColumnName = "LOGINNO", DbDataType = "varchar", Description = "登录名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 20, NotNull = true)] 10 public string LoginNo { get; set; } 11 12 [TableColumn(ColumnName = "USERNAME", DbDataType = "varchar", Description = "用户姓名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 20, NotNull = false)] 13 public string UserName { get; set; } 14 15 [TableColumn(ColumnName = "NICKNAME", DbDataType = "varchar", Description = "昵称", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 30, NotNull = false)] 16 public string NickName { get; set; } 17 18 [TableColumn(ColumnName = "TEL", DbDataType = "varchar", Description = "电话", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = 11, 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 }
运行起来看看获取的情况!