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 ...
随机推荐
- doubleclick protobuf file load to project
1,download protobuf file to local wget https://developers.google.com/ad-exchange/rtb/downloads/openr ...
- Cassandra基础
Apache Cassandra特性 Apache Cassandra由Facebook基于Amazon的Dynamo及其在Google的Bigtable上的数据模型设计开发的面相列的数据库,实现没有 ...
- spark aggregate函数详解
aggregate算是spark中比较常用的一个函数,理解起来会比较费劲一些,现在通过几个详细的例子带大家来着重理解一下aggregate的用法. 1.先看看aggregate的函数签名在spark的 ...
- 1. docker 在 macOS 中的架构 2. 在macOS系统中,docker pull 下来的镜像存储在哪里?
docker 在 macOS 中的架构: 在macOS中,docker的实现跟在其它Linux系统中略有不同,在其它Linux系统中,操作系统本身就是docker容器的宿主机,docker镜像都是直接 ...
- this、new,容易混淆的地方
this.new,容易混淆的地方 情况1 关系 情况2 new Foo() 等价于,推荐的写法是new Foo() new Foo new Foo() 不一样 Foo(), Foo()这种情况下,构造 ...
- 阿里云 qW3xT.4 挖矿病毒问题
查了一下.是个挖矿病毒,cpu 占用巨高 .杀了又有守护进程启动.网上有些杀死这个病毒的办法,大家可以试试.但是不确定能杀死. 建议直接重装系统. 然后,说说这货怎么传播的. 他通过redis .目前 ...
- Flume和Kafka整合安装
版本号: RedHat6.5 JDK1.8 flume-1.6.0 kafka_2.11-0.8.2.1 1.flume安装 RedHat6.5安装单机flume1.6:http://b ...
- 【转】mysql给root开启远程访问权限,修改root密码
好记性不如烂笔头,偶然用一直忘.... mysql给root开启远程访问权限,修改root密码 1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1) ...
- Python依赖打包发布详细
http://www.cnblogs.com/mywolrd/p/4756005.html 将Python脚本打包成可执行文件 Python是一个脚本语言,被解释器解释执行.它的发布方式: .py ...
- mysql_test
------------------ #/bin/sh binlogfile=$1 if [ ! -n $binlogfile ]thenecho "pls input your mysql ...