特性Attibute定义和使用
1.定义特性
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false,Inherited = false)]
public class TableNameAttribute:Attribute
{
private string _tableName;
public TableNameAttribute(string tableName)
{
_tableName = tableName;
} public string TableName {
get { return _tableName; }
}
}
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class ColumnNameAttribute:Attribute
{
private string _columnName;
public ColumnNameAttribute(string columnName)
{
_columnName = columnName;
} public string ColumnName
{
get { return _columnName; }
}
}
2.在Model上使用特性
[TableName("Category")]
public class Category:BaseEntity<long>
{
/// <summary>
/// 类别名称
/// </summary>
[ColumnName("Name")]
public string Name { get; set; }
/// <summary>
/// 类别编码
/// </summary>
[ColumnName("Code")]
public string Code { get; set; }
/// <summary>
/// 父类别编码
/// </summary>
public string ParentCode { get; set; }
/// <summary>
/// 类别链接
/// </summary>
public string Url { get; set; }
/// <summary>
/// 类别层级
/// </summary>
public int CategoryLevel { get; set; }
/// <summary>
/// 状态
/// </summary>
public StateEnums State { get; set; }
}
3.反射获取特性信息
[TestMethod]
public void AttributeTest()
{
Type type = typeof(Category);
//获取类class上的特性
var tableNameAttribute = (TableNameAttribute)type.GetCustomAttributes(typeof(TableNameAttribute), false).FirstOrDefault();
if (tableNameAttribute != null)
{
string tableName = tableNameAttribute.TableName;
}
//获取属性Property上的特性
foreach (var property in type.GetProperties())
{
var columnNameAttribute = (ColumnNameAttribute)property.GetCustomAttributes(typeof(ColumnNameAttribute), false).FirstOrDefault();
if(columnNameAttribute != null)
{
string columnName = columnNameAttribute.ColumnName;
}
}
}
特性Attibute定义和使用的更多相关文章
- JAVA8新特性——接口定义增强
JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ 接口定义增强 在JDK1.8以前,接口是定义的: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法 ...
- python.36的特性新定义初学者必看课程
一.Python3.6新特性 1.新的格局化字符串办法 <p "="">新的格局化字符串办法,即在一般字符串前增加 f 或 F 前缀,其效果相似于str.fo ...
- Attribute(一)——提前定义特性
在项目中接触到了Attribute,那么什么是Attribute,有些什么作用呢?这里来了解一下. 一.什么是Attribute Attribute 类将提前定义的系统信息或用户定义的自己定义信息与目 ...
- 【.net 深呼吸】自定义特性(Attribute)的实现与检索方法
在.net的各个语言中,尤其是VB.NET和C#,都有特性这一东东,具体的概念,大家可以网上查,这里老周说一个非标准的概念——特性者,就是对象的附加数据.对象自然可以是类型.类型成员,以及程序集. 说 ...
- C#中的 特性 详解(转载)
本篇幅转载于:http://www.cnblogs.com/rohelm/archive/2012/04/19/2456088.html C#中特性详解 特性提供了功能强大的方法,用于将元数据或声明信 ...
- C#回顾 –6.特性
1.特性是什么? Attribute 用来对类.属性.方法等标注额外的信息,贴一个标签(附着物) 通俗:给 类 或 类成员 贴一个标签,就像航空部为你的行李贴一个标签一样 个人理解,特性就是修饰 ...
- .Net内置特性Attribute介绍
特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...
- MVC中获取模型属性的Range和StringLength验证特性设置
MVC中的客户端及服务端模型验证信息都以ModelMetadata类型作为承载,在获得属性的ModelMetadata之后(还不知道怎么获取ModelMetadata的童鞋请自行恶补),我们可以轻松得 ...
- 【C#进阶系列】18 特性Attribute
这个东西有的叫定制特性,然而我喜欢直接叫特性,但是这样的话一些人不知道我说的是什么,如果我说是Attribute的话那么知道的或者用过的就都懂了. 还记得讲到枚举和位标志那一章,关于位标志,有一个[F ...
随机推荐
- centos7-centos6常用配置对比
设置(CentOS 6 vs CentOS 7)系统常用配置 ysvinit vs Upstart vs Systemd) 常见设置: 字符集CentOS 6方法:/etc/sysconfig/i1 ...
- react-native 编译 undefined is not an object (evaluating '_react2.PropTypes.func')
情况通报: 因为是我的二维码模块报错,提示报错代码如下 重要信息是下面的红色字体部分(Android 模拟器红屏) undefined is not an object (evaluating '_r ...
- 尼克的任务 dp 洛谷1280
蒟蒻表示老久没看过dp题目了,,挺水的一道dp题目都没想出来,,, 首先设dp[i]表示从开始到i时间的最大空闲时间,用vector to[x] 表示从x点开始的任务结束时间,cnt[x]表示从x开始 ...
- C++的hashmap和Java的hashmap
C++里面是这样的:typedef std::unordered_map<std::string,std::string> stringmap; std::unordered_map< ...
- 从头认识Spring-1.16 SpEl对集合的操作(1)-建立集合以及訪问集合的元素,以<util:list/>为例
这一章节我们来讨论一下怎样建立集合以及訪问集合的元素? 1.建立集合? (1)domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch ...
- linux+apache+mysql+php平台构建及环境配置
1.我使用的centos6.安装时已经选择安装apach.mysql,事实上在运行下列两行命令的时候又对其进行了更新.所以说装的时候能够不安装,免得浪费时间. yum install php-mysq ...
- FZOJ--2214--Knapsack problem(背包)
Problem 2214 Knapsack problem Accept: 5 Submit: 8 Time Limit: 3000 mSec Memory Limit : 32768 K ...
- Kali linux 2016.2(Rolling)里Metasploit的常用模块
端口扫描 auxiliary/scanner/portscanscanner/portscan/ack ACK防火墙扫描scanner/portscan/ftpbounce FTP跳端口扫描scann ...
- iOS菜鸟成长笔记(3)——斯坦福公开课学习(1)
一.iOS四层结构 1.Core OS 是用FreeBSD和Mach所改写的Darwin, 是开源.符合POSIX标准的一个Unix核心.这一层包含或者说是提供了整个iPhone OS的一些基础功能, ...
- OpenGL编程逐步深入(十一)组合变换
准备知识 在前面的几节教程中,我们已经提到过几种变换,为物体在3D世界中的移动提供的极大的灵活性.但是我们还有很多东西需要学习(如摄像机控制和透视投影),你可以已经猜到,我们需要將这些变换组合起来.在 ...