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)的更多相关文章

  1. Java8 新特性学习 Lambda表达式 和 Stream 用法案例

    Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...

  2. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  3. C#特性学习笔记二

    实例探讨: 自定义了一个特性类: [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)] class WahAttribute ...

  4. ES7/8新特性学习随笔

    随着每年EcmaScript都会为js带来一些新特性,带来更多美化的编程体验,今天就走进一下es2016/2017所带来的新特性 ES7新特性 includes() 指数操作符 ES8新特性 asyn ...

  5. java8新特性学习1

    java8增加了不少新特性,下面就一些常见的新特性进行学习... 1.接口中的方法 2.函数式接口 3.Lambda表达式 4.java8内置的四大核心函数式接口 5.方法引用和构造器引用 6.Str ...

  6. python切片、迭代、生成器、列表生成式等高级特性学习

    python高级特性 1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 切片 当我们要取一个list中的前n各元素时,如果前n个少的话,我们还可以一个一个的取,但是若前n个元 ...

  7. C#的特性学习

    转自:https://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html   特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类 ...

  8. 【Java语言特性学习之一】设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...

  9. java8新特性学习:函数式接口

    本文概要 什么是函数式接口? 如何定义函数式接口? 常用的函数式接口 函数式接口语法注意事项 总结 1. 什么是函数式接口? 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口 ...

  10. java8新特性学习:stream与lambda

    Streams api 对 Stream 的使用就是实现一个 filter-map-reduce 过程,产生一个最终结果,或者导致一个副作用(side effect). 流的操作类型分为两种: Int ...

随机推荐

  1. (惊艳)基于谷底最小值的阈值的图像分割(改进HSV中的H分量可以用imhist(H)提取)

    任务概述:将这张图片作为输入 , 然后抠出只有斑点的图片 灵感来源: 1. 黄色部分用绿色的掩盖掉得到图片B,然后A和B进行∩运算,相同的设置为0 2.统计单词的子母数,开辟一个26个元素的数组,进来 ...

  2. linux 文件管理操作入门

    mkdir -p /root/kali/bp/shell  一路创建文件夹直到生成文件夹shell,中间没有kali文件夹的话也会自动创建生成 tar解压缩 范例一:将整个 /etc 目录下的文件全部 ...

  3. 装饰模式Decorator Pattern

    1.主要优点 装饰模式的主要优点如下: (1) 对于扩展一个对象的功能,装饰模式比继承更加灵活性,不会导致类的个数急剧增加. (3) 可以对一个对象进行多次装饰,通过使用不同的具体装饰类以及这些装饰类 ...

  4. es高级部分

    1 关于机器 配置. 内存:上亿的数据一般需要64G内存的服务器.劲量不要使用小于32G 内存的服务器. cpu:es 对cpu 要求依赖不如内存.一般要求2-8 核就可以了. 磁盘:es 对磁盘依赖 ...

  5. npm安装教程(vue.js)

    https://www.cnblogs.com/goldlong/p/8027997.html 首先理清nodejs和npm的关系: node.js是javascript的一种运行环境,是对Googl ...

  6. tcpdump过滤某个端口

    一般我们使用Tcpdump时都是使用: Java代码   tcpdump -i ethx      www.2cto.com   下面这条命令就是查看80端口的访问量,进行排序,取前20位    Ja ...

  7. reduceByKey和groupByKey区别与用法

    在spark中,我们知道一切的操作都是基于RDD的.在使用中,RDD有一种非常特殊也是非常实用的format——pair RDD,即RDD的每一行是(key, value)的格式.这种格式很像Pyth ...

  8. 从Excel文件中读取内容

    从Excel文件中读取内容 global::System.Web.HttpPostedFileBase file = Request.Files["txtFile"]; strin ...

  9. Simple Logging Facade for Java 简单日志门面(Facade)

    SLF4J是为各种 loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现.Logging API实现既可以选择直接实现SLF4J接口的 ...

  10. mac os x 安装mysql 5.7

    一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL Community Server”下方有一个“download” ...