c#特性学习(1)
C#中的特性我认为可以理解为Java中的注解,见名知意,就是描述这个类或是属性的一个信息,是一个语法糖.原理运用的是反射,下面我来演示一下它的原理.其中引用了软谋的代码.
举一个栗子.我们在做codefirst的时候有时候类名和表名我们不想要一样的,那么就可以利用特性来指定表名.
先来一个UserModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 特性
{
public class User
{
public int Id { get; set; }
public string Account
{
set;
get;
}
/// <summary>
/// 密码
/// </summary>
public string Password
{
set;
get;
}
/// <summary>
/// EMaill
/// </summary>
public string Email
{
set;
get;
}
/// <summary>
/// 手提
/// </summary>
public string Mobile
{
set;
get;
}
/// <summary>
/// 企业ID
/// </summary>
public int? CompanyId
{
set;
get;
}
/// <summary>
/// 企业名称
/// </summary>
public string CompanyName
{
set;
get;
}
/// <summary>
/// 用户状态 0正常 1冻结 2删除
/// </summary>
public int? State
{
set;
get;
}
/// <summary>
/// 用户类型 1 普通用户 2管理员 4超级管理员
/// </summary>
public int? UserType
{
set;
get;
}
/// <summary>
/// 最后一次登陆时间
/// </summary>
public DateTime? LastLoginTime
{
set;
get;
} /// <summary>
/// 最后一次修改人
/// </summary>
public int? LastModifierId
{
set;
get;
}
/// <summary>
/// 最后一次修改时间
/// </summary>
public DateTime? LastModifyTime
{
set;
get;
}
}
}
UserModel
再来一个自定义特性类,使用特性需要继承Attribute抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 特性
{
public class TableAttribute:Attribute
{
private string _TableName = null; public TableAttribute(string tableName)
{
this._TableName = tableName;
} public string GetTableName()
{
return this._TableName;
}
}
}
TableAttribute
对UserModel添加扩展方法,这里运用反射,调用GetCustomAttributes方法获取自定义特性的数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 特性
{
public static class Extend
{
public static string GetTableName<T>(this T t) where T : new()
{
Type type = t.GetType();
object[] oAttributeList = type.GetCustomAttributes(true);
foreach (var item in oAttributeList)
{
if (item is TableAttribute)
{
TableAttribute attribute = item as TableAttribute;
return attribute.GetTableName();
}
} return type.Name;
}
}
}
Extend
对UserModel添加特性,可以省略Attribute后缀
调用方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 特性
{
class Program
{
static void Main(string[] args)
{
User user = new User
{
Id =
}; string name = user.GetTableName<User>(); Console.WriteLine(name);
Console.ReadKey();
}
}
}
查看结果
c#特性学习(1)的更多相关文章
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- java8 新特性学习笔记
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...
- C#特性学习笔记二
实例探讨: 自定义了一个特性类: [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)] class WahAttribute ...
- ES7/8新特性学习随笔
随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...
- java8新特性学习1
java8增加了不少新特性,下面就一些常见的新特性进行学习... 1.接口中的方法 2.函数式接口 3.Lambda表达式 4.java8内置的四大核心函数式接口 5.方法引用和构造器引用 6.Str ...
- python切片、迭代、生成器、列表生成式等高级特性学习
python高级特性 1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 切片 当我们要取一个list中的前n各元素时,如果前n个少的话,我们还可以一个一个的取,但是若前n个元 ...
- C#的特性学习
转自:https://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...
- 【Java语言特性学习之一】设计模式
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...
- java8新特性学习:函数式接口
本文概要 什么是函数式接口? 如何定义函数式接口? 常用的函数式接口 函数式接口语法注意事项 总结 1. 什么是函数式接口? 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口 ...
- java8新特性学习:stream与lambda
Streams api 对 Stream 的使用就是实现一个 filter-map-reduce 过程,产生一个最终结果,或者导致一个副作用(side effect). 流的操作类型分为两种: Int ...
随机推荐
- shell基础入门(一)
//获取输入内容 #!/bin/bash echo "What is your name?" read PERSON read -p "who are you name: ...
- tailor+ skipper 实现micro-frontends 简单试用
tailor 在Mosaic 框架中扮演fragment 模版layout的处理,后端fragment可以用任何服务编写 tailor 主要就是进行layout的处理.tailor的是类似facebo ...
- KMPlayer速度步进设置及快捷键设置和同级视频自动加入播放列表
速度步进设置 快捷键设置 同级视频自动加入播放列表
- System.Collections 学习
public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); } public inter ...
- sublime 安装插件出现问题
一. 解决package Install不能安装 If for some reason the console installation instructions do not work for ...
- springMVC--4种映射处理器handlerMapping
根据controller的name名称来映射寻找controller:BeanNameUrlHandlerMapping (默认) 1.1开启该映射:默认是开启的 <bean class=&q ...
- mina基础知识整理
一. 简介: Apache Mina Server 是一个网络通信应用框架,Mina 可以帮助我们快速开发高性能.高扩展性的网络通信应用,Mina 提供了事件驱动.异步(Mina 的异步 I ...
- php 内网/外网ip判断
工作需要判断ip是否是内网ip,本来想着使用正则自己写一个呢,后来发现php自带的有现成的函数[filter_var()](http://php.net/manual/zh/function.filt ...
- jmeter—PerfMon Metrics Collector(附java.io.IOException: Agent is unreachable via TCP错误解决办法)
jmeter—PerfMon Metrics Collector(附java.io.IOException: Agent is unreachable via TCP错误解决办法 转自https:// ...
- Redis sortedset有效集合数据结构
1. 增加一个有效集合 2. 查看元素个数 3. zscore 4. zcount 5. 返回指定元素的索引 zrank 6.zincrby 给元素a加90分 7. zrange查看范围