How to check if method has an attribute

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

I have an example class

public class MyClass{

    ActionResult Method1(){
        ....
    } 

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


        
5条回答
  •  鱼传尺愫
    2020-12-15 17:04

    If you make use of FluentAssertions you can do the following:

    var classInstance = new MyClass();
    Func method1 = classInstance.Method1;
    method1.GetMethodInfo().Should().BeDecoratedWith();
    
    Func method2 = classInstance.Method2;
    method2.GetMethodInfo().Should().BeDecoratedWith();
    
    Func method3 = classInstance.Method3;
    method3.GetMethodInfo().Should().BeDecoratedWith();
    

提交回复
热议问题