博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net core自定义特性操作
阅读量:5256 次
发布时间:2019-06-14

本文共 5767 字,大约阅读时间需要 19 分钟。

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

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

1 using System; 2  3 namespace TestDemo 4 { 5     ///  6     /// 表实体特性 7     ///  8     [AttributeUsage(AttributeTargets.Class, Inherited = false)] 9     public class TableAttribute : Attribute10     {        11         /// 12         /// 数据库表名称13         /// 14         public string TableName { get; set; }15         /// 16         /// 数据库表注释17         /// 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     ///  6     /// 列特性 7     ///  8     [AttributeUsage( AttributeTargets.Property , AllowMultiple = false)] 9     public class TableColumnAttribute : Attribute10     {11         /// 12         /// 列名称13         /// 14         public string ColumnName { get; set; }15         /// 16         /// 字段说明17         /// 18         public string Description { get; set; }19         /// 20         /// 是否是主键21         /// 22         public bool IsPrimaryKey { get; set; }23         /// 24         /// 主键是否自动增长25         /// 26         public bool IsPrimaryKeyAuto { get; set; }27         /// 28         /// 数据库数据类型29         /// 30         public string DbDataType { get; set; }31         /// 32         /// 字符串最大长度33         /// 34         public int MaxLength { get; set; }35         /// 36         /// 是否不可为空37         /// 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
();14 Console.WriteLine("User(TableName:" + table.TableName + "\r\n,Description:" + table.Description + ")");15 Console.WriteLine();16 17 TableColumnAttribute colId = GetTableColumnAttribute
("Id");18 Console.WriteLine("Id(ColumnName:" + colId.ColumnName 19 + "\r\n,DbDataType:" + colId.DbDataType20 + "\r\n,Description:" + colId.Description21 + "\r\n,IsPrimaryKey:" + colId.IsPrimaryKey22 + "\r\n,IsPrimaryKeyAuto:" + colId.IsPrimaryKeyAuto23 + "\r\n,MaxLength:" + colId.MaxLength24 + "\r\n,NotNull:" + colId.NotNull + ")");25 Console.WriteLine();26 27 TableColumnAttribute colName = GetTableColumnAttribute
("UserName");28 Console.WriteLine("UserName(ColumnName:" + colName.ColumnName29 + "\r\n,DbDataType:" + colName.DbDataType30 + "\r\n,Description:" + colName.Description31 + "\r\n,IsPrimaryKey:" + colName.IsPrimaryKey32 + "\r\n,IsPrimaryKeyAuto:" + colName.IsPrimaryKeyAuto33 + "\r\n,MaxLength:" + colName.MaxLength34 + "\r\n,NotNull:" + colName.NotNull + ")");35 36 Console.ReadLine();37 }38 39 ///
40 /// 获取表特性41 /// 42 ///
43 ///
44 public static TableAttribute GetTableAttribute
()45 {46 Type t = typeof(T);47 TableAttribute m = t.GetTypeInfo().GetCustomAttribute
();48 return m;49 }50 51 ///
52 /// 获取列特性53 /// 54 ///
55 ///
56 ///
57 public static TableColumnAttribute GetTableColumnAttribute
(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
();70 }71 }72 }73 74 return m;75 }76 }77 }
Program

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

转载于:https://www.cnblogs.com/moncci/p/6347144.html

你可能感兴趣的文章
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>
vue和react的区别
查看>>
第十一次作业
查看>>
负载均衡策略
查看>>
微信智能开放平台
查看>>
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
子网划分讲解及练习(一)
查看>>
c# 文件笔记
查看>>
第一页 - 工具的使用(webstorm)
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
D3.js 之 d3-shap 简介(转)
查看>>