C#的自定义属性AttributeUsage用法

 [AttributeUsage( // AttributeUsage是.net定义的特性的内置特性说明,可以叫特性的元特性。
        AttributeTargets.All,// 应用到的   程序元素[必选],使用时候如果是assembly或module的可以放
//置在代码中的任何地方
        AllowMultiple = true, // 同一个程序元素上是否可以使用多次[可选]
        Inherited = false)] // 是否可以继承,接口/类,如果是属性方法那么重载属性方法中也会影响[可选]

 // 描述如何使用一个自定义特性 SomethingAttribute


        //********自定义特性SomethingAttribute**************//
        public class SomethingAttribute : Attribute
        {
            private string name; // 名字
            private string data; // 日期
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            public string Data
            {
                get { return data; }
                set { data = value; }
            }
            public SomethingAttribute(string name)
            {
                this.name = name;
                this.name = name;
            }
        }
       
        [Something("类外特性", Data = "2018-06-18")]
        class Testone { }
        class TestTwo
        {

            [Something("类内特性", Data = "2018-06-18")]
            public string MyId { get; set; }


        }

应用


   /// <summary>
        /// 反射获得用户类外自定义属性
        /// </summary>
        /// <param name="t"></param>
        private static void PrintSomethingByAttribute(System.Type t)
        {
            string ss = $"\n类型的 System.Type 对象是:{t}";
            System.Console.WriteLine(ss);
            System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  //反射获得用户自定义属性  

            foreach (System.Attribute attr in attrs)
            {
                if (attr is SomethingAttribute)
                {
                    SomethingAttribute a = (SomethingAttribute)attr;
                    string str = $"   名称:{a.Name}, 日期: {a.Data}";
                    System.Console.WriteLine(str);
                }
            }
        }
        /// <summary>
        /// 反射获得类自定义内属性
        /// </summary>
        /// <param name="t"></param>
        private static void PrintSomethingByInfo(System.Type t)
        {
            string ss = $"\n类型的 System.Type 对象是:{t}";
            System.Console.WriteLine(ss);
            System.Reflection. PropertyInfo [] propertys = t.GetProperties()   ;//返回所有公共属性  
            if (propertys != null && propertys.Length > 0)  
            {
                foreach (System.Reflection.PropertyInfo p in propertys)
                {
                    object[] objAttrs = p.GetCustomAttributes(typeof(SomethingAttribute), true);//获取自定义特性  
                    //GetCustomAttributes(要搜索的特性类型,是否搜索该成员的继承链以查找这些特性)  
                  
                    if (objAttrs != null && objAttrs.Length > 0)
                    {
                        foreach (var m in objAttrs)
                        {
                            SomethingAttribute attr = m as SomethingAttribute;
                            Console.WriteLine("自定义特性Name:" + p.Name + ", 元数据name:" + attr.Name);
                        }
                    }

                                   };
            }
        }


  static void Main(string[] args)
        {
        //首先,通过typeof+类名也可以获得Type类型的对象。   
         Type t = typeof(Test);

           PrintSomethingByAttribute(t);
            PrintSomethingByInfo(t);

            t = typeof(TestTwo);
            PrintSomethingByAttribute(t);
            PrintSomethingByInfo(t);

            Console.ReadKey();
         //其次,利用type对象调用GetCustomAttributes方法,可以获得该类所使用的所有特性;
    //其中,该方法的参数表示是否搜索该类所继承的父类所使用的特性,false为不搜索;
        //当我们获取这些特性时,得到的是这些特性所生成的对象,但它们不是特性类的对象;
    //返回值为一个object类型的数组,保存所有的对象。
         

       }

结果

类型的 System.Type 对象是:ConsoleApplication2.Program+Testone
名称:类外特性, 日期: 2018-06-18

类型的 System.Type 对象是:ConsoleApplication2.Program+Testone

类型的 System.Type 对象是:ConsoleApplication2.Program+TestTwo

类型的 System.Type 对象是:ConsoleApplication2.Program+TestTwo
自定义特性Name:MyId, 元数据name:类内特性

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值