zoukankan      html  css  js  c++  java
  • 3语法基础_特性

    特性的作用,在进入你类,方法,字段,之前可以进行提前预判,增加额外功能

    1、什么是特性

      特性其实就是一个继承Attribute的类 使用之后可以影响到编译器的编译 如

      使用特性通过反编译工具其实就是在类的内部生成.custom

    2、 特性定义三部曲,1:定义特性类,2:实体类添加特性类,3:第三方类写特性逻辑

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    namespace MyAttribute
    {
        //AttributeTargets. 设置你的特性约束 规定使用在类 方法 字段 属性
        [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
        public class CutomAttribute : Attribute
        {
            public CutomAttribute()
            {
                Console.WriteLine("无参数构造函数");
            }
            public CutomAttribute(int i)
            {
                Console.WriteLine("Int类型构造函数");
            }
    
            public CutomAttribute(int i, string j)
            {
                Console.WriteLine("两个参数构造函数");
            }
            public string Remark { get; set; }
    
            public string Description;
    
            public void Show()
            {
                Console.WriteLine("通过反射调用特性中的方法");
            }
        }
     
    }
    定义特性类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAttribute
    {
    
        //CutomAttribute 可以简化名称 attribute可以省略
        [Cutom(234, "111")]
        [Cutom(123)]
        [Cutom(567, Remark = "111", Description = "111")]
        [CutomAttribute]
        public class Student
        {
            public int Id { get; set; }
             
            public string Name { get; set; }
             
            public void Study()
            {
                Console.WriteLine($"这里是{this.Name} ");
            }
             
            //[return:Cutom]
            public string Answer(string name)
            {
                return $"This is {name}";
            }
        }
    }
    定义实体类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyAttribute
    {
        public class InvokeCenter
        {
            public static void ManagerSudent<T>(T student) where T : Student
            {
                Console.WriteLine(student.Id);
                Console.WriteLine(student.Name);
                student.Answer("zzzz");
                Type type = student.GetType();//反射
                if (type.IsDefined(typeof(CutomAttribute), true)) // 先判断
                {
                    object[] aAttributeArry = type.GetCustomAttributes(typeof(CutomAttribute), true);
                    foreach (CutomAttribute attribute in aAttributeArry)
                    {
                        attribute.Show();
                    }
    
                    foreach (var prop in type.GetProperties())
                    {
                        if (prop.IsDefined(typeof(CutomAttribute), true)) // 先判断
                        {
                            object[] aAttributeArryprop = prop.GetCustomAttributes(typeof(CutomAttribute), true);
                            foreach (CutomAttribute attribute in aAttributeArryprop)
                            {
                                attribute.Show();
                            }
                        } 
                    } 
                    //特性方法
                    foreach (var method in type.GetMethods())
                    {
                        if (method.IsDefined(typeof(CutomAttribute), true)) // 先判断
                        {
                            object[] aAttributeArryMethod = method.GetCustomAttributes(typeof(CutomAttribute), true);
                            foreach (CutomAttribute attribute in aAttributeArryMethod)
                            {
                                attribute.Show();
                            }
                        }
    
                    }
    
                }
    
    
            }
        }
    }
    定义第三方类 反射+特性
  • 相关阅读:
    Apache ab压力测试
    2、Android自动测试之Monkey工具
    1、Monkey环境搭建
    解决IDEA中,maven依赖不自动补全的问题
    Centos7解决在同一局域网内无法使用ssh连接
    sql草稿
    mysql三表联合查询,结果集合并
    vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
    vue:使用不同参数跳转同一组件,实现动态加载图片和数据,以及利用localStorage和vuex持久化数据
    vue:解决使用param传参后,再次刷新页面会新增一个原有的tab
  • 原文地址:https://www.cnblogs.com/LZXX/p/13025070.html
Copyright © 2011-2022 走看看