阅读帖子“How to check if method has an attribute”后,我是解决让我保持清醒的问题的一步。
(我正在使用ASP.Net MVC 4)
这些界面:
public interface IFlyable
{
ActionResult Fly();
}
public interface IRunnable
{
ActionResult Run();
}
此抽象类:
public abstract class SuperHero : Controller
{
public void SavePeople()
{
}
}
此控制器:
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();
}
MethodHasAuthorizeAttribute(() => superHeroFlyable.Fly())
课程中的SuperHeroTest
来电{}返回false
时应该返回true
。
(班级Fly
中实施的方法SuperManController
具有属性Authorize
)。
我将方法Authorize
中的属性Fly
添加到IFlyable
,然后返回true
。
public interface IFlyable
{
[Authorize]
ActionResult Fly();
}
如何让MethodHasAuthorizeAttribute
检查实现而不是界面?
答案 0 :(得分:9)
通过对IfSuperHeroCanFlyMustHaveAuthorizeAttribute()方法的一些修改,您可以使其工作。
首先检查控制器是否实现了IFlyable接口。如果是这样,请获取控制器的Fly方法的MethodInfo。然后,您只需要检查返回的MethodInfo的属性。这样您就可以检查实现是否具有属性而不是接口。
以下工作正常:
[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();
}
}