【发布时间】:2017-01-05 19:37:27
【问题描述】:
是否可以将解决方案概括为适用于任何类型?
有一个很棒的solution 来断言方法上是否存在指定的属性:
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();
}
用法是这样的:
var sut = new SystemUnderTest();
var y = MethodHasAuthorizeAttribute(() => sut.myMethod());
Assert.That(y);
我们如何推广此解决方案并更改签名:
public static bool MethodHasAuthorizeAttribute(Expression<System.Action> expression)
到这样的事情:
public static bool MethodHasSpecifiedAttribute(Expression<System.Action> expression, Type specifiedAttribute)
是否可以将解决方案概括为适用于任何类型?
【问题讨论】:
标签: c# .net unit-testing visual-studio-2013 nunit