(精华)2020年8月9日 C#基础知识点 特性

一:基本使用执行特性中的方法

//----------------基本使用执行特性中的方法--------------------
Student student = new Student();
Student studentVip = new StudentVip()
{
  Id = 1,
  Name = "热爱学习",
  VipGroup = ".Net vip"
};
InvokeCenter.ManagerSudent<Student>(studentVip);
/// <summary> 
    /// 请不要使用这个了,请使用什么来代替  
    /// </summary>
    //[Obsolete("请不要使用这个了,请使用什么来代替")]//系统
    //[Serializable]//可以序列化和反序列化  
    [Cutom(234, "辣辣零")]
    [Cutom(123)]
    [Cutom(567, Remark = ".Net VIP", Description = ".Net VIP")]
    [CutomAttributeChild]
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
         
        public void Study()
        {
            Console.WriteLine($"这里是{this.Name}");
        }
         
        public string Answer(string name)
        {
            return $"This is {name}";
        }
    }
[Cutom(123, Remark = ".Net vip", Description = "vip 正在学习")]
    public class StudentVip : Student
    {
        [Cutom(123, Remark = ".Vip", Description = ".Net vip")]
        public string VipGroup { get; set; }

        [Cutom(123, Remark = ".Vip", Description = ".Net vip")]
        public void Homework()
        {
            Console.WriteLine("Homework");
        }

        [SalaryAttribute(1000000)]
        public long Salary { get; set; }
    }
public class InvokeCenter
    {
        public static void ManagerSudent<T>(T student) where T : Student
        {
            Console.WriteLine(student.Id);
            Console.WriteLine(student.Name);
            student.Answer("YZM");
            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();
                        }
                    }
                }
            }
        }
    }
[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("通过反射调用特性中的方法");
        }
    }

    public class CutomAttributeChild : CutomAttribute
    {
        public CutomAttributeChild() : base(345)
        {
        }
    }

二:获取枚举中特性说明

//----------------特性获取额外的信息--------------------
string remark1 = AttributeExtend.GetRemark(UserState.Normal);
string remark2 = AttributeExtend.GetRemark(UserState.Frozen);
string remark3 = UserState.Deleted.GetRemark(); //扩展方法
public static class AttributeExtend
    {
        public static string GetRemark(this Enum value)
        {
            Type type = value.GetType();
            var field = type.GetField(value.ToString());
            if (field.IsDefined(typeof(RemarkAttribute), true))
            {
                RemarkAttribute attribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute), true);
                return attribute.Remark;
            }
            else
            {
                return value.ToString();
            }
        }
    }
public enum UserState
    {
        /// <summary>
        /// 正常状态
        /// </summary>
        [RemarkAttribute("正常状态")]
        Normal = 0,

        /// <summary>
        /// 已冻结
        /// </summary>
        [RemarkAttribute("已冻结")] 
        Frozen = 1,

        /// <summary>
        /// 已删除
        /// </summary>
        [RemarkAttribute("已删除")]
        Deleted = 2
    }

三:特性如何增加额外方法

 bool flg = AttributeExtend.Validate(student);
public class AttributeExtend
    {
        public static bool Validate(StudentVip student)
        {
            Type type = student.GetType();

            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(SalaryAttribute), true)) // 这里先判断  是为了提高性能
                {
                    object ovale = prop.GetValue(student);
                    foreach (SalaryAttribute attribute in prop.GetCustomAttributes(typeof(SalaryAttribute), true)) // 获取特性的实例  上面先判断之后,再获取实例
                    {
                        return attribute.Validate(ovale);
                    }
                }
            }
            return false;
        }
    }
[AttributeUsage(AttributeTargets.Property)]
    public class SalaryAttribute : AbstratValidateAttribute
    {
        public long _Salary { get; set; }  // 公司的预算 800000

        public SalaryAttribute(long salary)
        {
            _Salary = salary;
        }

        public override bool Validate(object oValue)
        {
            return oValue != null && long.TryParse(oValue.ToString(), out long lValue) && lValue < _Salary;
        }

    }

猜你喜欢

转载自blog.csdn.net/aa2528877987/article/details/107900540