通过反射获取特性属性Id

本文通过一个.NET平台上的特性应用实例,展示了如何使用自定义特性来增强类的功能,并通过反射获取这些特性进行进一步的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CustomAttribute.cs

    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
    public class CustomAttribute : Attribute
    {
        public CustomAttribute()
        { }

        public CustomAttribute(string remark)
        { }

        public string Description;

        public int Id { get; set; }

        public void ShowRemark()
        { }
    }

    public class CustomChildAttribute : CustomAttribute
    {
    }

Student.cs

  [Custom("学员父类", Id = 3, Description = "这里是字段")]
    public class Student
    {
        [Custom("学员父类ID", Id = 4, Description = "这里是字段ID")]
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual void Study()
        { }
    }

    [Custom("vip学员", Id = 3, Description = "这里是付费学员")]
    public class StudentVip : Student
    {
        [Custom("学员父类ID", Id = 4, Description = "这里是字段ID")]
        public int LessonId { get; set; }
        public int Price { get; set; }

        [Custom("学员父类ID", Id = 4, Description = "这里是字段ID")]
        public override void Study()
        {
            Console.WriteLine("学习vip课程");
            //CustomAttribute attibute = new CustomAttribute();
            //attibute.ShowRemark();
        }

        //public int Description { get; set; }
    }

    public class StudentFree : Student
    {
        public int LessonId { get; set; }
        public override void Study()
        {
            Console.WriteLine("学习公开课");
        }
    }


 AttributeShow.cs
 public class AttributeShow
    {
        public static void ShowStudent<T>(T t) where T : Student
        {
            Type type = t.GetType();
            object[] oAttribuyteArray = type.GetCustomAttributes(typeof(CustomAttribute), true);
            if (oAttribuyteArray != null && oAttribuyteArray.Length > 0)
            {
                CustomAttribute attribute = oAttribuyteArray[0] as CustomAttribute;
                attribute.ShowRemark();
                t.Id = attribute.Id;
            }
            t.Study();

        }
    }

Program main

 class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班vip课程,今天的内容是特性和AOP");
                People people = new People();

                StudentVip vip = new StudentVip()
                {
                    Id = 11,
                    LessonId = 62618,
                    Name = "木偶",
                    Price = 2299
                };
                AttributeShow.ShowStudent<StudentVip>(vip);
                Console.WriteLine(vip.Id);


        
         
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }

截图:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值