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();
                        }
                    }

                }

            }


        }
    }
}
定义第三方类 反射+特性

 

posted @ 2020-06-01 14:37  12不懂3  阅读(189)  评论(0编辑  收藏  举报
创作不易,请勿抄袭,欢迎转载!