.net高级技术——Attribute

什么是Attribute?Attribute有什么用?

Attribute相当于标签,比如说我们在即将拆迁的房子上会画一个拆字,还比如说被查封的公司会有一个封条!这些都是标签。那么在C#中的标签是什么样子的呢?

常见的Attribute有:[Obsolete]、

在使用一些窗体控价的时候可以使用[ReadOnly]、[DisplayName]等等!

以后在很多框架里面还会有很多的Attribute

那么Attribute到底有什么用呢??

Attribute用来在代码中附加一些元信息、这些元信息可以被编译器、.NET Framework或者我们的程序所使用。方法、属性或者类上都可以标注Attribute

在Type、MethodInfo、PropertyInfo等上都可以调用object[] GetCustomAttributes(Type attributeType, bool inherit)获取标注的注解对象,因为同一个Attribute可能标注多次,所以返回值是数组

下面我们用一个案例来看看标签的使用

  class Program
    {
        static void Main(string[] args)
        {
           
            Person p1 = new Person();
            p1.Name = "张三";
            p1.Sex = "男";
            Type type = p1.GetType();
            object[] obsoleteAttrs = type.GetCustomAttributes(typeof(ObsoleteAttribute), false);
            if (obsoleteAttrs.Length > 0)
            {
                Console.WriteLine("该类已过时");
            }
            else
            {
                Console.WriteLine("该类没有过时");
            }
            Console.ReadKey();
        }
    }
    [Obsolete]
    public class Person
    {
        [DisplayName("姓名")]
        public string Name { get; set; }

        [DisplayName("性别")]
        public string Sex { get; set; }
    }

上面这个例子是判断类有没有过时(本质是有没有加Obsolete标签)

下面来举个例子 ,如果一个属性你想要显示中文名字的话,就要使用DisPlayName标签

            Type type = p1.GetType();
            string disName;
            foreach (var property in type.GetProperties())
            {
                string proName = property.Name;//获取属性名称
                //DisplayNameAttribute displayNameAttr = (DisplayNameAttribute)property.GetCustomAttribute(typeof(DisplayNameAttribute));//获取到属性的DisplayNameAttribute的相关值
                object[] disAtrr = property.GetCustomAttributes(typeof(DisplayNameAttribute), false);
                DisplayNameAttribute displayNameAttr = (DisplayNameAttribute)disAtrr[0];
                if (displayNameAttr == null)//判断该属性是否标记了该标签
                {
                    disName = null;
                }
                else
                {
                    disName = displayNameAttr.DisplayName;
                }
                object value = property.GetValue(p1);
                Console.WriteLine("属性名" + proName + "   中文名字<   " + disName + "   >  value=" + value);
            }

如果你想要再来个日语的该怎么弄呢?

接下来我们自定义一个标签

首先我们还是按照约定创建一个名为JapaneseDisPlayAttribute类,继承至Attribute

    class JapaneseDisPlayNameAttribute : System.Attribute
    {
        public virtual string DisplayName { get; }
        public JapaneseDisPlayNameAttribute(string displayName)
        {
            this.DisplayName = displayName;
        }
    }

然后我们在属性上加入标签

public class Person
    {
        [JapaneseDisPlayName("亚麻的")]
        [DisplayName("姓名")]
        public string Name { get; set; }

        [JapaneseDisPlayName("亚麻的")]
        [DisplayName("性别")]
        public string Sex { get; set; }
    }
 Type type = p1.GetType();
   string disName;
            string disNameJapanese;
            foreach (var property in type.GetProperties())
            {
                string proName = property.Name;//获取属性名称
                //DisplayNameAttribute displayNameAttr = (DisplayNameAttribute)property.GetCustomAttribute(typeof(DisplayNameAttribute));//获取到属性的DisplayNameAttribute的相关值
                object[] disAtrr = property.GetCustomAttributes(typeof(DisplayNameAttribute), false);
                object[] disArr1 = property.GetCustomAttributes(typeof(JapaneseDisPlayNameAttribute), false);
                DisplayNameAttribute displayNameAttr = (DisplayNameAttribute)disAtrr[0];
                JapaneseDisPlayNameAttribute JapanesedisplayNameAttr = (JapaneseDisPlayNameAttribute)disArr1[0];
                if (displayNameAttr == null)//判断该属性是否标记了该标签
                {
                    disName = null;
                    disNameJapanese = null;
                }
                else
                {
                    disName = displayNameAttr.DisplayName;
                    disNameJapanese = JapanesedisplayNameAttr.DisplayName;
                }
                object value = property.GetValue(p1);
                Console.WriteLine("属性名" + proName + "   中文名字<   " + disName + "   >  "+"日文名字<"+">" +disNameJapanese+""+ "value=" +value);
            }

如果我们以Attribute结尾的话,那么我们可以省略Attribute

标签本身是没有任何作用的,在作用的是使用标签的人和程序,去解析标签

比如墙上的拆字,如果我们不去按照它的意思去做,那么这个标签就毫无意义,但是真实生活中我们看到这个标签的时候,就知道这个房子是要拆迁了,挖土机就会来拆。

---本博客是学习以后记录知识,如有侵权,请联系删除!!!

おすすめ

転載: blog.csdn.net/qq_33407246/article/details/88973895