zoukankan      html  css  js  c++  java
  • 特性

    特性:一个直接或间接继承自Attribute的类,一般以Attribute结尾,声明时候可以省略掉

    //AttributeTargets表示修饰对象  AllowMultiple表示是否可以多次修饰
        [AttributeUsage(AttributeTargets.All,AllowMultiple =true)]
        [Obsolete("牛的",false)]//为false则是报警告,true为报错
        public class CustomAttribute:Attribute
        {
            //构造函数
            public CustomAttribute()
            {
    
            }
            //构造函数重载
            public CustomAttribute(int n)
            {
    
            }
            //属性
            public string ID { get; set; }
            //字段
            public string _id;
        }

     找被特性修饰的元素

                    //
                    Type type = typeof(Stu);
                    if (type.IsDefined(typeof(CustomAttribute), true))
                    {
                        CustomAttribute attribute=(CustomAttribute)type.GetCustomAttribute(typeof(CustomAttribute), true);
                    }
                    //属性
                    PropertyInfo property = type.GetProperty("属性名");
                    if (property.IsDefined(typeof(CustomAttribute), true))
                    {
                        CustomAttribute attribute = (CustomAttribute)property.GetCustomAttribute(typeof(CustomAttribute), true);
                    }
                    //方法
                    MethodInfo method = type.GetMethod("方法名");
                    if (method.IsDefined(typeof(CustomAttribute), true))
                    {
                        CustomAttribute attribute = (CustomAttribute)method.GetCustomAttribute(typeof(CustomAttribute), true);
                    }
                    //参数
                    ParameterInfo parameter = method.GetParameters()[0];
                    if (parameter.IsDefined(typeof(CustomAttribute), true))
                    {
                        CustomAttribute attribute = (CustomAttribute)parameter.GetCustomAttribute(typeof(CustomAttribute), true);
                    }
  • 相关阅读:
    5、Hystrix服务熔断(服务端)与服务降级(客户端)
    迷宫回溯问题
    4、负载均衡:Ribbon、Feign
    PHP-删除排序数组中的重复项
    MYSQL-连续出现的数字
    PHP
    MYSQL分数排名
    MYSQL查询第二高的薪水
    PHP算法之有效的括号
    PHP算法之电话号码的字母组合
  • 原文地址:https://www.cnblogs.com/TheLin/p/14337084.html
Copyright © 2011-2022 走看看