How to check if method has an attribute

后端 未结 5 1552
礼貌的吻别
礼貌的吻别 2020-12-15 16:32

I have an example class

public class MyClass{

    ActionResult Method1(){
        ....
    } 

    [Authorize]
    ActionResult Method2(){
       ....
    }         


        
5条回答
  •  北海茫月
    2020-12-15 16:52

    Find some sample where i find the methods in a class that have a specified attribute applied.

    private static void GetMethodInfo(object className)
            {
                var methods = className.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);
    
                foreach(var m in methods)
                {
                    var parameters = m.GetParameters();
                    var att = m.GetCustomAttributes(typeof (CustomAttribute), true);
                }
            }
    

    The parameter that is passed is an instance of a class. You can modify the code to suit your requirement which should be pretty easy.

提交回复
热议问题