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 ...
随机推荐
- Mysql清理二进制日志的技巧
1:二进制日志 二进制日志记录了所有的DDL(数据定义语言)语句和DML(数据操作语言)语句,但是不记录包括数据查询的语句.语句以"事件"的形式保存,它描述了数据的更改过程,此日志 ...
- 用java写的一个简易记事本
import java.awt.*; import java.awt.event.*; import java.io.*; public class NoteDemo { private Frame ...
- char , unsigned char 和 signed char 区别
ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.char相当于signed char或者unsigned char,但是这取决于编译器!这三种字符 ...
- centos7.2部署最新ELK 5.3
## 安装elasticsearch服务> 安装jdk 1.8 ```rpm -ivh jdk-8u101-linux-x64.rpmjava -version``` > 配置rpm `` ...
- 优化Servlet:(利用反射的思想)
1.创建BaseServlet (重写父类的service方法) package com.learning.web.servlet; import java.io.IOException; impor ...
- 关于php 高并发解决的一点思路
涉及抢购.秒杀.抽奖.抢票等活动时,为了避免超卖,那么库存数量是有限的,但是如果同时下单人数超过了库存数量,就会导致商品超卖问题.那么我们怎么来解决这个问题呢,我的思路如下(伪代码): sql1:查询 ...
- spring的MVC执行原理
spring的MVC执行原理 1.spring mvc将所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责对请求 进行真正的处理工作. 2.DispatcherSer ...
- C++ 编译报错discards qualifiers [-fpermissive]
声明了一个类 class Card { public: Card(const string&); int m_value; char m_suit; private: const static ...
- ES6入门
整理了ES6常用的一些语法,跟大家分享(promise.generator什么的还没有梳理清楚,后续再更新...) 1⃣️ 变量声明-let 与 const (首先关于测试结果:这里有个小问题,如果用 ...
- 【微信小程序】认识微信小程序
目前微信小程序已经支持个人版了 罗列一下微信开发的一些资料和工具 (如果你还不了解微信小程序如何操作请点击 新手教程 )里面罗列了 开发者工具 如何使用 和 微信的基本操作 很详细 一.微信开发者工 ...