c# - 如何检查方法是否具有属性(使用接口(interface)、转换和抽象)

标签 c# asp.net-mvc unit-testing reflection custom-attributes

阅读帖子“How to check if method has an attribute”后,我迈出了解决让我保持清醒的问题的一步。

我介绍情况:

(我正在使用 ASP.Net MVC 4)

这些接口(interface):

public interface IFlyable
{
    ActionResult Fly();
}    

public interface IRunnable
{
    ActionResult Run();
}

这个抽象类:

public abstract class SuperHero : Controller
{
    public void SavePeople()
    {
    }    
}

这个 Controller :

public class SuperManController : SuperHero,IFlyable,IRunnable {

    [Authorize]
    public ActionResult Fly(){
        // Flying...
    }    

    [Authorize]
    public ActionResult Run(){
        // Running...
    }    

}

这个抽象类(用于测试)

[TestClass]
public abstract class SuperHeroTest<TSuperHero>{

    protected abstract TSuperHero GetSuperHero();

    [TestMethod]
    public void IfSuperHeroCanFlyMustHaveAuthorizeAttribute(){

        var superHero=GetSuperHero();

        if(superHero is IFlyable)
        {
            var superHeroFlyable = (IFlyable) superHero;

            var have = MethodHasAuthorizeAttribute(() => superHeroFlyable.Fly());

            Assert.IsTrue(have);
        }

    }
}

最后这个类继承自SuperHeroTest来测试SuperManController:

[TestClass]
public class SuperManControllerTest : SuperHeroTest<SuperManController>{

    private SuperManController _superManController;

    public SuperManControllerTest(){
        _superManController=new SuperManController();
    } 


    protected override SuperManController GetSuperHero()
    {
        return _superManController;
    }

}

方法 MethodHasAuthorizeAttribute 是:(来自上面的帖子)

public static MethodInfo MethodOf(Expression<System.Action> expression)
{
    MethodCallExpression body = (MethodCallExpression)expression.Body;
    return body.Method;
}

public static bool MethodHasAuthorizeAttribute( Expression<System.Action> expression )
{
    var method = MethodOf( expression );

    const bool includeInherited = false;
    return method.GetCustomAttributes(typeof(AuthorizeAttribute),includeInherited).Any();
}

我的问题是:

SuperHeroTest 类中的调用 MethodHasAuthorizeAttribute(() => superHeroFlyable.Fly()) 在应该返回 时返回 false是的

(类 SuperManController 中实现的方法 Fly 具有属性 Authorize )。

我在 Fly 方法中添加了属性 AuthorizeIFlyable 中,然后返回 true

public interface IFlyable
{
    [Authorize]
    ActionResult Fly();
}  

如何让 MethodHasAuthorizeAttribute 检查实现而不是接口(interface)?

最佳答案

通过对 IfSuperHeroCanFlyMustHaveAuthorizeAttribute() 方法进行一些修改,您可以使其正常工作。

首先检查你的 Controller 是否实现了 IFlyable 接口(interface)。如果是这样,获取 Controller 的 Fly 方法的 MethodInfo。然后您只需要检查返回的 MethodInfo 的属性。这样您就可以检查实现是否具有属性而不是接口(interface)。

以下工作正常:

[TestClass]
public abstract class SuperHeroTest<TSuperHero>
{
    protected abstract TSuperHero GetSuperHero();

    [TestMethod]
    public void IfSuperHeroCanFlyMustHaveAuthorizeAttribute()
    {
        var superHero = GetSuperHero();

        if (superHero is IFlyable)
        {
            var superHeroFlyable = superHero;
            var method = typeof (TSuperHero).GetMethod("Fly");
            var hasAttribute = 
                method.GetCustomAttributes(typeof (AuthorizeAttribute), false).Any();
            Assert.IsTrue(hasAttribute);
        }
    }

    public static MethodInfo MethodOf(Expression<System.Action> expression)
    {
        var body = (MethodCallExpression)expression.Body;
        return body.Method;
    }

    public static bool MethodHasAuthorizeAttribute(Expression<System.Action> expression)
    {
        var method = MethodOf(expression);
        const bool includeInherited = false;
        return method.GetCustomAttributes(
            typeof(AuthorizeAttribute), includeInherited).Any();
    }
}

关于c# - 如何检查方法是否具有属性(使用接口(interface)、转换和抽象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658949/

相关文章:

angularjs - Jasmine 的代码覆盖率中未涵盖的语句

c# - 如何刷新wpf数据触发器?

c# - 如何创建一个程序终止后自动删除的临时文件?

c# - 从属性 getter 返回数组的替代方法

c# - 在 OnAuthorization 之前执行的 ASP.NET MVC 全局或 BaseController ActionFilter

javascript - 使用 JavaScript 在页面上使用 Canvas 图像路径的 MVC

javascript - 如何在javascript中创建cookie并在mvc3的 Controller 中使用

c# - 显式空检查与空合并运算符的编译器评估?

.net - VS2010 中的 Assert.AreEqual() 异常

angular - 如何窥探可观察的属性(property)