C# 特性(Attribute)
个人定义:不侵入对象的情况下,添加对象附注信息。
官方定义:将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模 块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。提供的信息也称为元数据,元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。
.Net 框架提供了三种预定义特性:
- AttributeUsage
- Conditional
- Obsolete
示例: 读取枚举值上加的 特性信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyAttribute
{
[Obsolete("这是一个过期特性")]
public enum EnumState
{
[Remark("我是打开")]
//[Remark("")]
Open =,
[Remark("我是关闭")]
Close=
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace MyAttribute
{
//1.特性的适用范围 2.特性是否允许用多次 3.特性是否被派生类继承
[AttributeUsage(AttributeTargets.All,AllowMultiple =true, Inherited =true)]
//被调用时执行条件编译,取决于指定的值(如“DEBUG”,"RELEASE"...)
[Conditional("DEBUG")]
public class RemarkAttribute:Attribute
{
public string remark; public string Remark
{
get { return remark; }
} public RemarkAttribute(string remark)
{
this.remark = remark;
}
} public static class RemarkExtend
{
public static string RemarkDescription(this EnumState state)
{
Type type = typeof(EnumState);
FieldInfo info= type.GetField(state.ToString());
object[] remarkCustomAttribute= info.GetCustomAttributes(typeof(RemarkAttribute),false);
if(remarkCustomAttribute != null && remarkCustomAttribute.Length>)
{
RemarkAttribute remarkattribute = remarkCustomAttribute[] as RemarkAttribute;
return remarkattribute.Remark;
}
else
{
return state.ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyAttribute
{
class Program
{
static void Main(string[] args)
{
EnumState state = EnumState.Open;
Console.WriteLine(state.RemarkDescription());
Console.Read();
}
}
}
至此,关于特性的学习就到此结束了,谢谢您的阅读。
特性还有很多高级的用法,博主只是小试牛刀,希望本文能够帮到你,当然若有不完善地方,欢迎斧正。
参考MSDN:https://msdn.microsoft.com/zh-cn/library/system.attribute(VS.80).aspx
C# 特性(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 ...
随机推荐
- 07 The VC Dimension
当N大于等于2,k大于等于3时, 易得:mH(N)被Nk-1给bound住. VC维:最小断点值-1/H能shatter的最大k值. 这里的k指的是存在k个输入能被H给shatter,不是任意k个输入 ...
- Kubernetes DNS 简介
环境 $ sudo lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16 ...
- SQL模糊查询条件的四种匹配模式
执行数据库查询时,有完整查询和模糊查询之分. 一般模糊语句格式如下: SELECT 字段 FROM 表 WHERE 某字段 LIKE 条件 其中关于条件,SQL提供了四种匹配模式: 1.% :表示任意 ...
- 1029. Median
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- 关于/var/run/docker.sock
译者按: 这篇博客介绍了什么是/var/run/docker.sock,以及如何使用/var/run/docker.sock与Docker守护进程通信,并且提供了两个简单的示例.理解这些,我们就可以运 ...
- meta 整理
< meta > 元素 概要 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索引擎(关键词),或其他 we ...
- 【2017-04-24】winform基础、登录窗口、窗口属性
一.winform基础 客户端应用程序:C/S 客户端应用程序可以操作用户电脑中的文件,代码要在用户电脑上执行,吃用户电脑配置. 窗体是由控件和属性做出来的 控件:窗体里所放的东西."视图 ...
- tomcat的环境搭建
tomcat搭建过程还是比较简单的,只需要安装好jdk,然后配置好环境变量,最后把tomcat安装上开启就可以了. 首先下载jdk,然后把下载下来的jdk放到/usr/local下,然后用rpm -i ...
- C语言的一些基础
一.C语言基础: 1.1.main函数是入口函数,用于进行link. 1.2..sln是解决方案的管理文件. 1.3.int:32位.short:16位.long:32位.long long:64位. ...
- 十二个 ASP.NET Core 例子
原文地址:http://piotrgankiewicz.com/2017/04/17/asp-net-core-12-samples/ 作者:Piotr Gankiewicz 翻译:杨晓东(Savor ...