特性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 ...
随机推荐
- nginx编译支持HTTP2.0
nginx编译支持HTTP2.0 nginx编译支持HTTP2.0 wget https://www.openssl.org/source/openssl-1.1.0i.tar.gz #openssl ...
- keepalived的功能及DR模式搭建笔记
一.HA集群中的相关术语 1.节点(node) 运行HA进程的一个独立主机,称为节点,节点是HA的核心组成部分,每个节点上运行着操作系统和高可用软件服务,在高可用集群中,节点有主次之分,分别称之为主节 ...
- caioj 1157 线性筛选素数
注意这道题开得非常大,有2*1e7 自己可以养成一种习惯,如果数据是很容易的话,可以自己手动输入极限数据来测试自己的程序 #include<cstdio> #include<algo ...
- Java中四种复制数组的方法
JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...
- 作为一个程序员怎么通过android开发赚钱
上面是一个程序员通过Android开发每天的收入,信则有! 自己学安卓差不多,有一年了.我本来是从事javaweb开发的,可能学习安卓上手会快点.其实安卓没有那难 .首先开发安卓程序,要有一个,开 ...
- 关于functioncharts饼状图篇
关于functioncharts饼状图(仅限饼状图) TODO: 1.饼状图没有数据情况下,显示:no data to display 2,解决的方法:自己定义处理.显示图像或其他内容
- 迷宫求解_数据结构c语言版
#include <iostream> #include <string> #include <cstdio> #include <cstdlib> # ...
- hdu_1394,线段树求逆序数
http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #include< ...
- 递归神经网络——就是解决AST这样的问题
原文:https://zybuluo.com/hanbingtao/note/626300 有时候把句子看做是词的序列是不够的,比如下面这句话『两个外语学院的学生』: 上图显示了这句话的两个不同的语法 ...
- CUDA学习笔记(四)
昨天一直在写ben的作业.总结一下周一的cuda情况. cuda程序需要用到一些设置的参数,如argv[],另外cuda读入文件特别苛刻,只能采用C的方式,而且对w+,r的使用只有试通才行. 卧底天外 ...