C# 如何断言方法具有指定的属性
是否可以将解决方案推广到任何类型?C# 如何断言方法具有指定的属性,c#,.net,unit-testing,visual-studio-2013,nunit,C#,.net,Unit Testing,Visual Studio 2013,Nunit,是否可以将解决方案推广到任何类型? public static MethodInfo MethodOf(Expression<Action> expression) { MethodCallExpression body = (MethodCallExpression)expression.Body; return body.Method; } public static bool MethodHasAttribute(Expression<Action>
public static MethodInfo MethodOf(Expression<Action> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static bool MethodHasAttribute(Expression<Action> expression, Type attributeType)
{
var method = MethodOf(expression);
const bool includeInherited = false;
return method.GetCustomAttributes(attributeType, includeInherited).Any();
}
断言方法上是否存在指定属性有一个很好的方法:
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();
}
我们如何推广此解决方案并将签名从:
public static bool MethodHasAuthorizeAttribute(Expression<System.Action> expression)
公共静态bool方法hasAuthorizeAttribute(表达式)
对这样的事情:
var sut = new SystemUnderTest();
var y = MethodHasAuthorizeAttribute(() => sut.myMethod());
Assert.That(y);
public static bool MethodHasSpecifiedAttribute(Expression<System.Action> expression, Type specifiedAttribute)
var sut = new SystemUnderTest();
y = MethodHasAttribute<AuthorizeAttribute>(() => sut.myMethod());
That(y);
public static bool methodhassspecifiedattribute(表达式,类型specifiedAttribute)
可以将解决方案推广到任何类型吗?公共静态方法信息方法(表达式)
public static MethodInfo MethodOf(Expression<Action> expression)
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
public static bool MethodHasAttribute(Expression<Action> expression, Type attributeType)
{
var method = MethodOf(expression);
const bool includeInherited = false;
return method.GetCustomAttributes(attributeType, includeInherited).Any();
}
{
MethodCallExpression body=(MethodCallExpression)expression.body;
返回体。方法;
}
公共静态bool MethodHasAttribute(表达式,类型attributeType)
{
var方法=MethodOf(表达式);
const bool includeInhered=false;
return方法.GetCustomAttributes(attributeType,includeInherited).Any();
}
或使用泛型:
public static bool MethodHasAttribute<TAttribute>(Expression<Action> expression)
where TAttribute : Attribute
{
var method = MethodOf(expression);
const bool includeInherited = false;
return method.GetCustomAttributes(typeof(TAttribute), includeInherited).Any();
}
公共静态bool MethodHasAttribute(表达式)
其中:属性
{
var方法=MethodOf(表达式);
const bool includeInhered=false;
return方法.GetCustomAttributes(typeof(tatAttribute),includeInherited).Any();
}
你会这样称呼它:
var sut = new SystemUnderTest();
var y = MethodHasAuthorizeAttribute(() => sut.myMethod());
Assert.That(y);
public static bool MethodHasSpecifiedAttribute(Expression<System.Action> expression, Type specifiedAttribute)
var sut = new SystemUnderTest();
y = MethodHasAttribute<AuthorizeAttribute>(() => sut.myMethod());
That(y);
var sut=new SystemUnderTest();
y=MethodHasAttribute(()=>sut.myMethod());
即(y);
公共静态MethodInfo MethodOf(表达式)
{
MethodCallExpression body=(MethodCallExpression)expression.body;
返回体。方法;
}
公共静态bool MethodHasAttribute(表达式,类型attributeType)
{
var方法=MethodOf(表达式);
const bool includeInhered=false;
return方法.GetCustomAttributes(attributeType,includeInherited).Any();
}
或使用泛型:
public static bool MethodHasAttribute<TAttribute>(Expression<Action> expression)
where TAttribute : Attribute
{
var method = MethodOf(expression);
const bool includeInherited = false;
return method.GetCustomAttributes(typeof(TAttribute), includeInherited).Any();
}
公共静态bool MethodHasAttribute(表达式)
其中:属性
{
var方法=MethodOf(表达式);
const bool includeInhered=false;
return方法.GetCustomAttributes(typeof(tatAttribute),includeInherited).Any();
}
你会这样称呼它:
var sut = new SystemUnderTest();
var y = MethodHasAuthorizeAttribute(() => sut.myMethod());
Assert.That(y);
public static bool MethodHasSpecifiedAttribute(Expression<System.Action> expression, Type specifiedAttribute)
var sut = new SystemUnderTest();
y = MethodHasAttribute<AuthorizeAttribute>(() => sut.myMethod());
That(y);
var sut=new SystemUnderTest();
y=MethodHasAttribute(()=>sut.myMethod());
即(y);