posts - 52,comments - 30,views - 13万

写个方法去获取被特性(Attribute)标记的类,并且获取标记的属性值

复制代码
using OneLove.Core.ExtendedEnum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OneLove.Core.ExtendedAttribute
{
    public class SchedulingAttribute : Attribute
    {
        private string Url;
        private HttpMethods Method;
        private string Description;
        public SchedulingAttribute(string url, HttpMethods method, string description = null)
        {
            Url = url;
            Method = method;
            Description = description;
        }

        public List<SchedulinEntity> GetSchedulingAttributeList()
        {
            System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(typeof(SchedulingAttribute));
            System.Type[] types = asm.GetExportedTypes();
            List<SchedulinEntity> list = new List<SchedulinEntity>();
            foreach (Type type in types)
            {
                var attrs = type.GetCustomAttributes<SchedulingAttribute>();
                foreach (var attr in attrs)
                {
                    if (attr is SchedulingAttribute)
                    {
                        list.Add(new SchedulinEntity()
                        {
                            Description = attr.Description,
                            Method = attr.Method,
                            Url = attr.Url,
                            ApiClassName = type.Name
                        });
                    }
                }
            }
            return list;
        }


    }


    public class SchedulinEntity
    {
        public string Url { get; set; }
        public HttpMethods Method { get; set; }
        public string Description { get; set; }
        public string ApiClassName { get; set; }
    }
}
复制代码

 

posted on   白码一号  阅读(370)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示