C#特性的介绍及应用场景
/// <summary>
/// 描述实体映射的数据表
/// </summary>
public class TableNameAttribute : Attribute
{
public TableNameAttribute()
{ }
public string TableName { get; set; }
} [TableName(TableName = "Users")]
public class User
{
public int Id { get; set; }
public string Name { get; set; }
} class Program
{
public static void Main(string[] args)
{
User user = new User();
Type t = user.GetType();
object[] customAttrs = t.GetCustomAttributes(true);
for (int i = ; i < customAttrs.Length; i++)
{
if (customAttrs[i] is TableNameAttribute)
{
TableNameAttribute tnAttr = (TableNameAttribute)customAttrs[i];
Console.WriteLine(tnAttr.TableName);
}
}
}
}
6.2 做数据验证
public class IntValidateAttribute : Attribute
{
/// <summary>
/// 最小值
/// </summary>
private int minValue { get; set; }
/// <summary>
/// 最大值
/// </summary>
private int maxValue { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
public IntValidateAttribute(int minValue, int maxValue)
{
this.minValue = minValue;
this.maxValue = maxValue;
}
/// <summary>
/// 检验值是否合法
/// </summary>
/// <param name="checkValue"></param>
/// <returns></returns>
public bool Validate(int checkValue)
{
return checkValue >= minValue && checkValue <= maxValue;
}
} public class User
{
[IntValidate(, )]
public int Id { get; set; }
public string Name { get; set; }
} public class BaseDal
{
public static string Insert<T>(T model)
{
Type modelType = typeof(T);
//获取类型的所有属性
PropertyInfo[] propertyInfos = modelType.GetProperties(); bool boIsCheck = true;
//循环所有属性
foreach (var property in propertyInfos)
{
//获取属性的所有特性
object[] attrs = property.GetCustomAttributes(true);
if (property.PropertyType.Name.ToLower().Contains("int"))
{
foreach (var attr in attrs)
{
if (attr is IntValidateAttribute)
{
IntValidateAttribute intValidate = (IntValidateAttribute)attr;
//执行特性的验证逻辑
boIsCheck = intValidate.Validate((int)property.GetValue(model));
}
}
}
if (!boIsCheck)
{
break;
}
}
if (boIsCheck)
{
return "验证通过,插入数据库成功";
}
else
{
return "验证失败";
}
}
}
class Program
{
public static void Main(string[] args)
{
string msg = BaseDal.Insert<User>(new User() { Id = , Name = "lvcc" });
Console.WriteLine(msg);
}
}
6.3 添加说明信息并获取,同6.1
/// <summary>
/// 备注特性
/// </summary>
public class RemarkAttribute : Attribute
{
private string Remark { get; set; }
public RemarkAttribute(string Remark)
{
this.Remark = Remark;
}
public string GetRemark()
{
return this.Remark;
}
} public enum ESex
{
[Attributes.Remark("男的")]
boy=
,
[Attributes.Remark("女的")]
girl =
} /// <summary>
/// 提供扩展方法
/// </summary>
public static class EnumExtension
{
public static string GetRemark(this Enum model)
{
if (model is ESex)
{
Type type = typeof(ESex);
FieldInfo fi = type.GetField(model.ToString()); object[] attributes = fi.GetCustomAttributes(true);
foreach (var attr in attributes)
{
if (attr is RemarkAttribute)
{
RemarkAttribute remark = (RemarkAttribute)attr;
return remark.GetRemark();
}
}
} return string.Empty;
}
} class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ESex.boy.GetRemark());
}
}
C#特性的介绍及应用场景的更多相关文章
- Redis 6.0 新特性 ACL 介绍
Redis 6.0 新特性 ACL 介绍 Intro 在 Redis 6.0 中引入了 ACL(Access Control List) 的支持,在此前的版本中 Redis 中是没有用户的概念的,其实 ...
- Redis常用数据类型介绍、使用场景及其操作命令
Redis常用数据类型介绍.使用场景及其操作命令 本文章同时也在cpper.info发布. Redis目前支持5种数据类型,分别是: 1.String(字符串) 2.List(列表) 3.Hash(字 ...
- ExtJS的4.1新特性简要介绍
ExtJS的4.1新特性简要介绍 一.动态加载机制 Ext.require动态加载任何类,并且会加载依赖类: 二.新类系统 •类定义:ExtJS4.0以后应入了Ext.define方法,他通过类的字符 ...
- Apache Spark 2.2.0 新特性详细介绍
本章内容: 待整理 参考文献: Apache Spark 2.2.0新特性详细介绍 Introducing Apache Spark 2.2
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_6-2.申请微信支付介绍和不同场景的支付方式
笔记 2.申请微信支付介绍和不同场景的支付方式 简介:介绍微信商户平台,支付方式和使用场景,申请微信支付流程 1.什么是微信商户平台: ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
笔记 第六章 微服务网关zuul开发实战 1.微服务网关介绍和使用场景 简介:讲解网关的作用和使用场景 (画图) 1)什么是网关 API Gateway,是系 ...
- MySQL主从复制介绍:使用场景、原理和实践
MySQL主从复制介绍:使用场景.原理和实践 MySQL数据库的主从复制方案,和使用scp/rsync等命令进行的文件级别复制类似,都是数据的远程传输,只不过MySQL的主从复制是其自带的功能,无需借 ...
- Java最近版本新特性使用介绍
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 在阅读<Thinking in Java>的过程中,并发这一章出现不少新特性,工作中也有 ...
- 【memcache缓存专题(1)】memcache的介绍与应用场景
简介 Memcached是一个高性能的分布式的内存对象缓存系统,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各 ...
随机推荐
- Android开发之自己定义Spinner样式的效果实现(源码实现)
android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定须要为了切合整个应用的风格,改动我们的Spinner样式.系统给我们提供了两种常 ...
- 浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自己主动弹出搜索提示
对于通过用户输入关键词实现自己主动弹出相关搜索结果,这里本人给两种解决方式,用于两种不同的情形. 常见方法是在数据库里建一个用户搜索关系表,然后通过用户搜索框输入的keyword异步调用数据表中的相关 ...
- Android 通过ADB Wireless无线调试应用
使用数据线调试应用难免不方便,本篇博客介绍使用ADB Wireless工具.当手机和电脑处在同一网络下.实现无线调试应用. ADB Wireless可以让手机用无线来代替USB连接.而使用ADB工具的 ...
- inter 也支持linux开发了
http://www.intel.cn/content/www/cn/zh/intelligent-systems/bay-trail/atom-processor-e3800-family-over ...
- js实现replaceAll功能
js中没有原生的replaceAll 方法. function replaceAll(str , replaceKey , replaceVal){ var reg = new RegExp(repl ...
- Interpret bytes as packed binary data
7.1. struct — Interpret bytes as packed binary data — Python 3.6.5 documentation https://docs.python ...
- u-boot支持LCD显示(基于TQ2440)【转】
本文转载自:http://www.cnblogs.com/pengdonglin137/p/4633877.html u-boot支持LCD显示(基于TQ2440) 阅读目录(Content) 平 ...
- YTU 1076: SPY
1076: SPY 时间限制: 1 Sec 内存限制: 128 MB 提交: 6 解决: 6 题目描述 The National Intelligence Council of X Nation ...
- ThreadLocal工具类的使用(隔离思想)
ThreadLocal不是用来解决共享对象的多线程访问问题的, 通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象, ...
- 【转】浏览器中输入url后发生了什么
原文地址:http://www.jianshu.com/p/c1dfc6caa520 在学习前端的过程中经常看到这样一个问题:当你在浏览器中输入url后发生了什么?下面是个人学习过程中的总结,供个人复 ...