Due to certain reasons, in our ASP.NET web application, it is not recommended to use Response.Redirect("something", True)
. It should be used with False for the endResponse
parameter. We want to enforce this with a custom FxCop rule.
I've managed to find the usages of Response.Redirect, but now I want to find the value of the endResponse
argument. How can I do this?
We're using the following code:
public override ProblemCollection Check(Member member)
{
var method = member as Method;
if (method != null)
{
foreach (var instruction in method.Instructions)
{
switch (instruction.OpCode)
{
case OpCode.Call:
case OpCode.Callvirt:
case OpCode.Newobj:
var call = (Method) instruction.Value;
if (call == null)
{
break;
}
if (call.Name.Name != "Redirect")
{
break;
}
if (call.Parameters.Count == 1)
{
//Redirect(url)
var resolution = GetResolution();
var problem = new Problem(resolution);
Problems.Add(problem);
}
if (call.Parameters.Count == 2)
{
VisitStatements(call.Body.Statements);
}
break;
default:
break;
}
}
}
return Problems;
}
public override void VisitExpression(Expression expression)
{
var methodCall = expression as MethodCall;
if (methodCall == null)
{
return;
}
foreach (var operand in methodCall.Operands)
{
if (operand.Type.Name.Name == "Int16" || operand.Type.Name.Name == "Int32" || operand.Type.Name.Name == "Int64")
{
var literal = operand as Literal;
if (literal != null && literal.Value is int)
{
var literalValue = (int)literal.Value;
if (literalValue == 1)
{
var resolution = GetResolution();
var problem = new Problem(resolution);
Problems.Add(problem);
}
}
}
}
}
I've used Introspector and thought the endResponse
parameter was an integer behind the scenes, but I'm not so sure anymore. Anyway, there don't seem to be any booleans in the methodCall.Operands
.
Has anyone ever had a similar situation, where you need to check the actual value of a parameter that's passed to a method?