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();
                            }
                        }
    
                    }
    
                }
    
    
            }
        }
    }
    定义第三方类 反射+特性
  • 相关阅读:
    服务器消息机制实现记录
    转载SQL经典代码按某一字段分组取最大(小)值所在行的数据
    记录js获取当前URL
    (原创)xilinx IP建立向导创建的目录和文件都是做什么的?由错误ERROR:HDLCompiler:Instantiating <xx> from unknown module <xx>引发的思考
    [转]NTFS3G的安装和配置
    (原创)Notepad++怎么实现双视图/双窗口?
    (原创)Quartus硬件工程路径改变,nios工程该怎么办?
    (原)verilog中的reg类型变量,一定会综合出触发器吗?
    (Windows)使用纯净版本的系统碟安装系统后没有网卡驱动怎么办?
    [转]NIOS_II的Boot过程分析
  • 原文地址:https://www.cnblogs.com/LZXX/p/13025070.html
Copyright © 2011-2022 走看看