C# 特性(Attribute)

栏目: ASP.NET · 发布时间: 7年前

内容简介: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 =1,
        [Remark("我是关闭")]
        Close=2
    }
}
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>0)
            {
                RemarkAttribute remarkattribute = remarkCustomAttribute[0] 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

本文永久更新链接地址 http://www.linuxidc.com/Linux/2017-06/144703.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大数据技术原理与应用

大数据技术原理与应用

林子雨 / 人民邮电出版社 / 2015-8-1 / 45.00

大数据作为继云计算、物联网之后IT行业又一颠覆性的技术,备受关注。大数据处不在,包括金融、汽车、零售、餐饮、电信、能源、政务、医疗、体育、娱乐等在内的社会各行各业,都融入了大数据的印迹,大数据对人类的社会生产和生活必将产生重大而深远的影响。 大数据时代的到来,迫切需要高校及时建立大数据技术课程体系,为社会培养和输送一大批具备大数据专业素养的高级人才,满足社会对大数据人才日益旺盛的需求。本书定......一起来看看 《大数据技术原理与应用》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器