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

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

 using System;

 namespace TestDemo
{
/// <summary>
/// 表实体特性
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class TableAttribute : Attribute
{
/// <summary>
/// 数据库表名称
/// </summary>
public string TableName { get; set; }
/// <summary>
/// 数据库表注释
/// </summary>
public string Description { get; set; } public TableAttribute()
{
TableName = string.Empty;
Description = string.Empty;
}
}
}

表特性

 using System;

 namespace TestDemo
{
/// <summary>
/// 列特性
/// </summary>
[AttributeUsage( AttributeTargets.Property , AllowMultiple = false)]
public class TableColumnAttribute : Attribute
{
/// <summary>
/// 列名称
/// </summary>
public string ColumnName { get; set; }
/// <summary>
/// 字段说明
/// </summary>
public string Description { get; set; }
/// <summary>
/// 是否是主键
/// </summary>
public bool IsPrimaryKey { get; set; }
/// <summary>
/// 主键是否自动增长
/// </summary>
public bool IsPrimaryKeyAuto { get; set; }
/// <summary>
/// 数据库数据类型
/// </summary>
public string DbDataType { get; set; }
/// <summary>
/// 字符串最大长度
/// </summary>
public int MaxLength { get; set; }
/// <summary>
/// 是否不可为空
/// </summary>
public bool NotNull { get; set; } public TableColumnAttribute()
{
ColumnName = string.Empty;
Description = string.Empty;
IsPrimaryKey = false;
IsPrimaryKeyAuto = false;
DbDataType = "varchar";
MaxLength = ;
NotNull = false;
}
}
}

表字段特性

 namespace TestDemo
{
[Table(Description = "用户表", TableName = "USER")]
public class User
{
[TableColumn(ColumnName = "ID", DbDataType = "int", Description = "主键", IsPrimaryKey = true, IsPrimaryKeyAuto = true, MaxLength = , NotNull = true)]
public int Id { get; set; } [TableColumn(ColumnName = "LOGINNO", DbDataType = "varchar", Description = "登录名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = true)]
public string LoginNo { get; set; } [TableColumn(ColumnName = "USERNAME", DbDataType = "varchar", Description = "用户姓名", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
public string UserName { get; set; } [TableColumn(ColumnName = "NICKNAME", DbDataType = "varchar", Description = "昵称", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
public string NickName { get; set; } [TableColumn(ColumnName = "TEL", DbDataType = "varchar", Description = "电话", IsPrimaryKey = false, IsPrimaryKeyAuto = false, MaxLength = , NotNull = false)]
public string Tel { get; set; }
}
}

用户表

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

 using System;
using System.Reflection;
using System.Text; namespace TestDemo
{
public class Program
{
public static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); TableAttribute table = GetTableAttribute<User>();
Console.WriteLine("User(TableName:" + table.TableName + "\r\n,Description:" + table.Description + ")");
Console.WriteLine(); TableColumnAttribute colId = GetTableColumnAttribute<User>("Id");
Console.WriteLine("Id(ColumnName:" + colId.ColumnName
+ "\r\n,DbDataType:" + colId.DbDataType
+ "\r\n,Description:" + colId.Description
+ "\r\n,IsPrimaryKey:" + colId.IsPrimaryKey
+ "\r\n,IsPrimaryKeyAuto:" + colId.IsPrimaryKeyAuto
+ "\r\n,MaxLength:" + colId.MaxLength
+ "\r\n,NotNull:" + colId.NotNull + ")");
Console.WriteLine(); TableColumnAttribute colName = GetTableColumnAttribute<User>("UserName");
Console.WriteLine("UserName(ColumnName:" + colName.ColumnName
+ "\r\n,DbDataType:" + colName.DbDataType
+ "\r\n,Description:" + colName.Description
+ "\r\n,IsPrimaryKey:" + colName.IsPrimaryKey
+ "\r\n,IsPrimaryKeyAuto:" + colName.IsPrimaryKeyAuto
+ "\r\n,MaxLength:" + colName.MaxLength
+ "\r\n,NotNull:" + colName.NotNull + ")"); Console.ReadLine();
} /// <summary>
/// 获取表特性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static TableAttribute GetTableAttribute<T>()
{
Type t = typeof(T);
TableAttribute m = t.GetTypeInfo().GetCustomAttribute<TableAttribute>();
return m;
} /// <summary>
/// 获取列特性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyName"></param>
/// <returns></returns>
public static TableColumnAttribute GetTableColumnAttribute<T>(string propertyName)
{
TableColumnAttribute m = null; Type t = typeof(T);
PropertyInfo[] arryProperty = t.GetProperties();
if (arryProperty != null)
{
foreach (PropertyInfo p in arryProperty)
{
if (p.Name == propertyName)
{
m = p.GetCustomAttribute<TableColumnAttribute>();
}
}
} return m;
}
}
}

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. 以多进程读取oss符合条件的数据为例,综合使用多进程间的通信、获取多进程的数据

    import datetime import sys import oss2 from itertools import islice import pandas as pd import re im ...

  2. VUE环境搭建及打包上线

    1.vue2.0新手填坑攻略之使用vue-cli搭建vue项目开发环境到项目发布 https://blog.csdn.net/u010020858/article/details/72865101 2 ...

  3. (转) IP子网划分

    原文:http://blog.csdn.net/birdie_l/article/details/77994610  子网划分公式计算法 实例一 实例二 心算思路总结: B类公式算法举例: 总结:此表 ...

  4. LeetCode 887.鸡蛋掉落(C++)

    每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去. 你知道存在楼层 F ,满足 0 <= F <= N 任何从高于 F 的楼层落下的鸡蛋都会碎,从 F 楼层或比它低的楼层落下的 ...

  5. WEB服务器、网站、域名、IP地址、DNS服务器之间的关系

    域名首先指向你的服务器,这个过程叫解析.  服务器分成好多小块,每小块叫一个空间或者一个虚拟主机.  所以当你输入你的域名以后,服务器收到你域名的访问信息,但不知道要打开这么多个小块中的那一个.所以要 ...

  6. pat1050. String Subtraction (20)

    1050. String Subtraction (20) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...

  7. Postman+Newman+Jenkins APItest自动化集成测试

    postman做接口测试简单易用很容易上手,但是接口越来越多,每次手动点击runner进行测试不灵活,最近就研究了下newman,利用newman实现接口的自动化测试,但是每次需要命令行操作也不方便, ...

  8. 使用Zxing生成一维码和二维码

    首先引用zxing.dll 到项目中引用 using System; using System.Collections.Generic; using System.Drawing; using Sys ...

  9. hibernate课程 初探一对多映射1-1 课程简介

    hibernate 常见映射类型 one-to-many many-to-one one-to-one many-to-many

  10. springmvc+spring+mybatis+sqlserver----插入一条新数据

    <insert id="addOneMsg" parameterType="java.util.Map"> INSERT INTO PDA_JWL_ ...