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表,它能够用来存储各 ...
随机推荐
- URL编码总结
URL编码总结 URL是Universal Resource Locator的简称.翻译过来那就是统一资源定位符,好吧,我们常常会俗称为网页地址. 一个URL的格式一般是这种:协议 ...
- UNIX网络编程学习(9)--getsockname和getpeername的用法及实例(转)
getsockname和getpeername #include <sys/socket.h>int getsockname(int sockfd, struct sockaddr *lo ...
- 十分简洁的手机浏览器 lydiabox
没有地址栏,没有工具栏.web app无需下载.无需安装.无需更新,加入即用:再也不用记住网址.更不用输入网址--一款这样极简极方便的浏览器,你想要吗? 我们做了一个十分简洁的手机浏览器,这个浏览器也 ...
- android 怎样将主菜单图标改成按安装时间排序
1. 在 LauncherModel.java 中增加例如以下代码, 假设是KK Launcher3 ApplicationInfo要替换为AppInfo public static final Co ...
- js实现replaceAll功能
js中没有原生的replaceAll 方法. function replaceAll(str , replaceKey , replaceVal){ var reg = new RegExp(repl ...
- maximize_window fullscreen_window minimize_window
# Licensed to the Software Freedom Conservancy (SFC) under one# or more contributor license agreemen ...
- ABAP 创建和调用WebService
1.创建WebService ① SE37创建RFC函数 ② SE80创建企业服务-service provider-existing ABAP object 或SE37-实用程序-创建Web服务 ③ ...
- 在ubuntu18.0下安装qt4.7以及qt-creator安装过程中遇到的坑
最近的嵌入式Linux系统上要做课程设计= =要用贼老贼老的qt4.7,配环境踩坑都费了我1天时间.....所以记录下来,希望能给和我遇到相同问题的朋友一点帮助 apt-get install g++ ...
- 关于mysqld_safe
昨天花了一天时间写了mysql的源码安装,比较蛋疼.其中对于mysqld_safe尤其不理解,因为使用apt-get安装几乎中间不需要什么配置,只需要service mysql start即可,但是源 ...
- 洛谷 P1072 Hankson 的趣味题 —— 质因数分解
题目:https://www.luogu.org/problemnew/show/P1072 满足条件的数 x 一定是 a1 的倍数,b1 的因数,a0/a1 与 x/a1 互质,b1/b0 与 b1 ...