译文链接:https://www.infoworld.com/art...

Attribute 在 C# 中是一个十分弱小的个性,它可能给你的程序集增加元数据信息。

Attribute 实际上是一个对象,它能够与以下元素中的任何一个相关联: 程序集、类、办法、委托、枚举、事件、字段、接口、属性和构造,它会在这些对象上做信息申明,当程序运行之后,你能够通过反射来获取关联到这些对象上的 Attribute 信息,换句话说:你能够通过 Atrribute 向程序集注入一些额定信息,而后在运行时通过反射来获取,attribute 个别由 名字 + 一些可选参数 形成, attribute 名字对应着 atrribute 类。

你能够利用 attribute 去校验你的业务model的正确性, attribute 有两种:内置 + 自定义, 前者是 .net framework 框架的组成部分,后者须要通过继承 System.Attribute 类来实现自定义。

当初来看看代码怎么写,Obsolete 个性用来标记一个办法是过期的,这个过期的意思是:你不应该再应用这个办法了,将来框架也会将其剔除,目前也存在其代替计划。其实在第三方框架中有很多这样的例子,上面的代码片段展现了如何在办法顶部应用 Obsolete 个性。

[Obsolete("This method is obsolete...")]public static void DoSomeWork(){}

如果你在程序中调用了这个办法,当你编译代码时,在 Visual Studio 输入窗口中会当初一些正告信息,如下图:

当然,如果你肯定要漠视它也是能够的,当初,如果你心愿你的开发共事不容许调用这个办法,那如何去限定呢?哈哈,能够应用 Obsolete 的第二个参数,这个参数是可选的,上面是 DoSomeWork() 办法的批改版本,请留神这是一个 Boolean 型参数。

[Obsolete("This method is obsolete...", true)] public static void DoSomeWork() { }

当把 true 给了这个可选参数后,再次编译代码,你会发现代码基本编译不通过,是不是完满的解决了你的问题,是吧! 截图如下:

自定义 attribute

这一大节咱们来看一下如何去实现自定义的 attribute,要想自定义实现,能够创立一个类并继承 System.Attribute 类即可,如下代码所示:

using System;public class CustomAttribute : Attribute{}

要想限定 CustomAttribute 的应用,能够用 AttributeUsage 类去标记,这个类蕴含了如下属性: ValidOnAllowMultiple,Inherited 等等,这些标记都能够限定 CustomAttribute 的应用。

上面的代码片段展现了 CustomAttribute 的批改版本,这个类应用构造函数去给外部的公有 string 赋值,代码仅仅用于演示目标。

    [AttributeUsage(AttributeTargets.All)]    public class CustomAttribute : Attribute    {        private string text;        public CustomAttribute(string text)        {            this.Text = text;        }        public string Text { get => text; set => text = value; }    }

当然你也能够按需去指定这些 AttributeTargets,如下代码所示:

    [AttributeUsage(AttributeTargets.Class |    AttributeTargets.Constructor |    AttributeTargets.Field |    AttributeTargets.Method |    AttributeTargets.Property,    AllowMultiple = true)]    public class CustomAttribute : Attribute    {        private string text;        public CustomAttribute(string text)        {            this.Text = text;        }        public string Text { get => text; set => text = value; }    }

接下来你能够用反射来获取利用到对象上的所有attributes,代码如下:

        static void Main(string[] args)        {            MemberInfo memberInfo = typeof(CustomAttribute);            object[] attributes = memberInfo.GetCustomAttributes(true);            for (int i = 0, j = attributes.Length; i < j; i++)            {                Console.WriteLine(attributes[i]);            }        }

接下来我筹备将 CustomAttribute 类利用到 上面的 SomeClass 类上。

[CustomAttribute("Hello World...")]public class SomeClass{}

能够着重看下 CustomAttribute 是如何安插在 SomeClass 上的,而且我还传递了一个 Hello World... 字符串给它,上面的代码展现了如何将 CustomAttribute 中的 Text 属性打印进去。

    class Program    {       static void Main(string[] args)        {            MemberInfo memberInfo = typeof(SomeClass);            object[] attributes = memberInfo.GetCustomAttributes(true);            foreach (object attribute in attributes)            {                CustomAttribute customAttribute = attribute as CustomAttribute;                if (customAttribute != null)                    Console.WriteLine("Text = {0}", customAttribute.Text);                else                    Console.WriteLine();            }        }    }    [CustomAttribute("Hello World...")]    public class SomeClass    {    }        [AttributeUsage(AttributeTargets.Class |    AttributeTargets.Constructor |    AttributeTargets.Field |    AttributeTargets.Method |    AttributeTargets.Property,    AllowMultiple = true)]    public class CustomAttribute : Attribute    {        private string text;        public CustomAttribute(string text)        {            this.Text = text;        }        public string Text { get => text; set => text = value; }    }

更多高质量干货:参见我的 GitHub: dotnetfly