NET 特性(Attribute)
NET 特性(Attribute)
转自 博客园(Fish)
特性(Attribute):是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。
您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号([ ])来描述的。
特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。
.Net 框架提供了两种类型的特性:预定义特性和自定义特性。
1.自定义特性:Net 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。
创建并使用自定义特性包含四个步骤:
- 声明自定义特性
- 构建自定义特性
- 在目标程序元素上应用自定义特性
- 通过反射访问特性
/// <summary>
/// 别名特性
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class AliasAttribute : Attribute
{
/// <summary>
/// 别名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 别名类型
/// </summary>
public Type Type { get; set; }
/// <summary>
/// 别名名称,类型默认为string
/// </summary>
/// <param name="name"></param>
public AliasAttribute(string name)
{
Name = name;
Type = typeof(string);
}
/// <summary>
/// 别名名称,类型为传入类型
/// </summary>
/// <param name="name"></param>
public AliasAttribute(string name, Type type)
{
Name = name;
Type = type;
}
}
2.在目标程序元素上应用自定义特性
public class UserDto
{
/// <summary>
/// 姓名
/// </summary>
[Alias("name", typeof(string))]
public string Name { get; set; }
/// <summary>
/// 电话
/// </summary>
[Alias("phone", typeof(string))]
public string Phone { get; set; }
/// <summary>
/// 备注
/// </summary>
[Alias("remark", typeof(string))]
public string Remark { get; set; }
}
3.通过反射访问特性
// 获取type
var userType = typeof(UserDto);
// 获取类中所有公共属性集合
var PropertyArr = userType.GetProperties();
foreach (var itemProperty in PropertyArr)
{
// 获取属性上存在AliasAttribute的数组
var customAttributesArr = itemProperty.GetCustomAttributes(typeof(AliasAttribute), true);
if (customAttributesArr.Any())
{
// 获取特性
var first = customAttributesArr.FirstOrDefault();
}
else {
// 不存在特性
}
}
NET 特性(Attribute)的更多相关文章
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
- C# 知识特性 Attribute
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...
- 区分元素特性attribute和对象属性property
× 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...
- .Net内置特性Attribute介绍
特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
- 如何获取类或属性的自定义特性(Attribute)
如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...
- C# 知识特性 Attribute,XMLSerialize,
C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...
- c#特性attribute:
特性是被编译到metadata中, 是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...
随机推荐
- 帝国cms提高网站网页打开速度的手段
1.减少页面HTTP请求数量 2.使用CDN(Content Delivery Network)网络加速 3.添加文件过期或缓存头 4.服务器开启gzip压缩 5.css格式定义放置在文件头部 6.J ...
- Java关键字之abstract、final、static用法
abstract:即抽象的,可以修饰类.方法: 修饰类:当有一个方法为抽象方法时,这个类就是抽象类,抽象类不能被new,它是一个不完整的类. 修饰方法:这个方法就是抽象的,即只能方法的定义,没有方法的 ...
- 性能篇系列—stream详解
Stream API Java 8集合中的Stream相当于高级版的Iterator Stream API通过Lambda表达式对集合进行各种非常便利高效的聚合操作,或者大批量数据操作 Stream的 ...
- JavaScript初探 五
JavaScript 初探 七 JavaScript 数据类型 基本的值类型 字符串(String) 数 字(Number) 布尔值(Boolean) 对 象(Object) 函 数(Function ...
- BayaiM__Oracle ASM操作管理
BayaiM__Oracle ASM操作管理 BayaiM__Oracle ASM操作管理 ...
- emacs speedbar功能介绍
emacs speedbar功能介绍 speedbar启动命令M-x speedbar,效果如下: speedbar是一个frame,它会遮挡你工作中的buffer.鼠标左键点击,或者敲回车,都会自动 ...
- OpenGL基础代码整理
3-1:画点,连成线 // OPENGL.cpp : Defines the entry point for the console application. // #include "st ...
- aiomysql
aiomysql: import aiomysql import asyncio async def aiomysql_test(): loop = asyncio.get_event_loop() ...
- java加密类
java.security.KeyStore KeyStore ks = KeyStore.getInstance(type); KeyStore ks = KeyStore.getInstance( ...
- LG4170/BZOJ1260 「CQOI2007」涂色 区间DP
区间DP 发现可以转化为区间包含转移. 考虑区间\([l,r]\),分为两种情况. \(col[l]=col[r]\) 此时相当于在涂\([l,r-1]\)或\([l+1,r]\)顺带着涂掉 \[f( ...