C# 通过反射获取方法/类上的自定义特性

  1.所有自定义属性都必须继承System.Attribute 

   2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾

自定义属性QuickWebApi

 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
    public class QuickWebApiAttribute: Attribute
    {
        public QuickWebApiAttribute(string _serviceCode,string _controllerName,string _actionName)
        {
            ServicesCode = _serviceCode;
            ControllerName = _controllerName;
            ActionName = _actionName;
        }

        public string ServicesCode{ get; set; }
        public string ControllerName { get; set; }
        public string ActionName { get; set; }

        
    }

接口类

    public interface ISchool
    {
        [QuickWebApi("SchoolServices", "School", "GetSchoolAll")]
        List<School> GetSchoolAll();
        [QuickWebApi("SchoolServices", "School", "GetSchoolListById")]
        List<School> GetSchoolListById(string schoolId);
    }

具体实现

   static void Main(string[] args)
        {
      

            Type t = typeof(ISchool);
            //获取方法特性中ActionName为GetSchoolAll的特性
            var b = t.GetMethods().Single(p => CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(p).ActionName == "GetSchoolAll");
            QuickWebApiAttribute  qu= CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(b);
            //循环遍历
            foreach (MemberInfo p in t.GetMethods())
            {
                object[] Attribute1 = p.GetCustomAttributes(true);
                object[] Attribute2 = p.GetCustomAttributes(typeof(QuickWebApiAttribute), false);
                //获取遍历结果
                //QuickWebApiAttribute att = CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(p);
                string a = "";
            }


            Console.ReadKey();
        }

知识扩展

 1 public enum AttributeTargets  
 2    {  
 3        // 摘要:  
 4        //     可以对程序集应用特性。  
 5        Assembly = 1,  
 6        //  
 7        // 摘要:  
 8        //     可以对模块应用特性。  
 9        Module = 2,  
10        //  
11        // 摘要:  
12        //     可以对类应用特性。  
13        Class = 4,  
14        //  
15        // 摘要:  
16        //     可以对结构应用特性,即值类型。  
17        Struct = 8,  
18        //  
19        // 摘要:  
20        //     可以对枚举应用特性。  
21        Enum = 16,  
22        //  
23        // 摘要:  
24        //     可以对构造函数应用特性。  
25        Constructor = 32,  
26        //  
27        // 摘要:  
28        //     可以对方法应用特性。  
29        Method = 64,  
30        //  
31        // 摘要:  
32        //     可以对属性应用特性。  
33        Property = 128,  
34        //  
35        // 摘要:  
36        //     可以对字段应用特性。  
37        Field = 256,  
38        //  
39        // 摘要:  
40        //     可以对事件应用特性。  
41        Event = 512,  
42        //  
43        // 摘要:  
44        //     可以对接口应用特性。  
45        Interface = 1024,  
46        //  
47        // 摘要:  
48        //     可以对参数应用特性。  
49        Parameter = 2048,  
50        //  
51        // 摘要:  
52        //     可以对委托应用特性。  
53        Delegate = 4096,  
54        //  
55        // 摘要:  
56        //     可以对返回值应用特性。  
57        ReturnValue = 8192,  
58        //  
59        // 摘要:  
60        //     可以对泛型参数应用特性。  
61        GenericParameter = 16384,  
62        //  
63        // 摘要:  
64        //     可以对任何应用程序元素应用特性。  
65        All = 32767,  
66    }  

猜你喜欢

转载自www.cnblogs.com/happygx/p/9066714.html