写个方法去获取被特性(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; } } }
标签:string,C#,System,Method,标识,查找,using,public,Description From: https://www.cnblogs.com/OneSeting/p/17354374.html