.net特性(个人笔记)

dui前言

个人理解的特性。对标 简单的AOP编程

就是在对于的 类、方法、属性等上面声明一个标签--->然后利用反射的知识对标签进行解析-->进行某些特殊处理的判断。 

实现过程

类/方法声明特性

    /// 
    /// 自定义Custom特性
    /// 
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    public class CustomAttribute : Attribute
    {
        /// 
        /// 无参构造函数
        /// 
        public CustomAttribute()
        {

        }

        /// 
        /// 有参构造函数
        /// 
        /// 
        public CustomAttribute(string description)
        {
            this.Description = description;
        }

        /// 
        /// 属性
        /// 
        public string Description { get; set; }

        /// 
        /// 字段
        /// 
        public string Remark = null;

        public void Show()
        {
            Console.WriteLine("This Is CustomAttribute");
        }
    }

对类使用特性 


     [Obsolete("请不要使用该类了,该类已经过时了")]
    [Custom("这是Custom自定义特性")]
    //, Custom, Custom(), Custom(Remark = "备注")
    public class Student
     {
         public int Id { get; set; }
 
         /// 
         /// 在属性上面使用Custom特性
         /// 
         [Custom("这是Name属性")]
         public string Name { get; set; }
 
         public string Accont { get; set; }
 
         public long QQ { get; set; }
 
         /// 
         /// 在方法和参数上面使用Custom特性
         /// 
         /// 
         /// 
        [Custom("这是Answer方法")]
        [return: Custom()] //方法的返回值应用特性
        public string Answer([Custom("这是方法参数")] string name)
        {
            var ce = $"This is {name}";

            return ce;

        }
    }

写一个对特性的反射解析方法

    /// 
    /// 特性扩展
    /// 
    public static class AttributeExtend
    {
        /// 
        /// 验证类
        /// 
        /// 
        /// 
        /// 

        public static bool ValidateClass(T t) {

            // Type type = typeof(Student); //或者使用student.GetType();
            Type type = t.GetType();

            // 找到类型上面的特性    type.IsDefined表示找类型上面的特性
            if (type.IsDefined(typeof(CustomAttribute), true))//检查有没有  性能高
           {
                //GetCustomAttribute 获取特性 type.GetCustomAttribute表示找到类型上面定义的特性,表示调用构造函数创建一个CustomAttribute类型的对象
                CustomAttribute attribute = (CustomAttribute)type.GetCustomAttribute(typeof(CustomAttribute), true);
                attribute.Show();
            }

            return true;
        }


        public static bool ValidateFun(T t) {
            Type type = t.GetType();
            #region 获取Answer()方法上面定义的特性
            // 获取Answer方法
            MethodInfo method = type.GetMethod("Answer");
                       if (method.IsDefined(typeof(CustomAttribute), true))
                            {
                                CustomAttribute attribute = (CustomAttribute)method.GetCustomAttribute(typeof(CustomAttribute), true);
                                Console.WriteLine($"{attribute.Description}_{attribute.Remark}");
                                attribute.Show();
                            }
        
            #endregion


            #region 获取参数定义的特性
          ParameterInfo parameter = method.GetParameters()[0];
                        if (parameter.IsDefined(typeof(CustomAttribute), true))
                           {
                               CustomAttribute attribute = (CustomAttribute)parameter.GetCustomAttribute(typeof(CustomAttribute), true);
                               Console.WriteLine($"{attribute.Description}_{attribute.Remark}");
                               attribute.Show();
                           }
           
            #endregion

            #region 获取返回值定义的特性
           ParameterInfo returnParameter = method.ReturnParameter;
                      if (returnParameter.IsDefined(typeof(CustomAttribute), true))
                          {
                              CustomAttribute attribute = (CustomAttribute)returnParameter.GetCustomAttribute(typeof(CustomAttribute), true);
                              Console.WriteLine($"返回值{attribute.Description}_{attribute.Remark}");
                              attribute.Show();
                          }

            #endregion

            return true;
        }
           
        /// 
        /// 验证属性
        /// 
        /// 
        /// 
        /// 
        public static bool Validate(T t)
        {
            Type type = t.GetType();
            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(BaseAttribute), true))
                {
                    var oValue = prop.GetValue(t, null);

                    foreach (BaseAttribute item in prop.GetCustomAttributes(typeof(BaseAttribute), true))//获取字段所有的特性
                    {
                        if (!item.Validate(oValue))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }

前期准备做完了,看看实现

类、方法、属性等上面声明一个标签--->然后利用反射的知识对标签进行解析-->进行某些特殊处理的判断。可以结合自己的理解想想应用场景

        static void Main(string[] args)
        {

            Student student = new Student();
            student.Accont="";
            //验证类特性,触发特性里面的方法
            AttributeExtend.ValidateClass(student);

            //验证方法特性,触发特性里面的方法
            AttributeExtend.ValidateFun(student);
          //检查特性,然后触发属性里面对应的处理
            //验证方法特性,触发特性里面的方法
            var result = AttributeExtend.Validate(student);
            Console.WriteLine("Hello World!");
}

.net特性(个人笔记)_第1张图片

 以上是自定义特性,.net还有很多封装特性的,感兴趣可以去查询

特性是 Fitter过滤器切面编程的底层知识,有人感兴趣的话,下篇文章详细介绍Fitter过滤器切面编程

你可能感兴趣的:(.net,c#)