custom fxcop rule to check is default present in switch
Hi,
I wrote a custom fxcop rule to check Is Default present in FxCop. After some days when i checked it i found that it was not working properly.
Do give any any input on how to check is default present in a Switch statement or not.
I have attached below the code that i wrote previously;
publicoverride ProblemCollection Check(Member member)
{
if (member.NodeType == NodeType.Method)
{
Method method = memberas Method;
Instruction instruction =null;
InstructionList instructions = method.Instructions;
for(int i=0;i<instructions.Length;i++)
{
instruction = instructions
;
if(instruction.OpCode == OpCode.Switch||instruction.OpCode == OpCode.Beq||instruction.OpCode == OpCode.Beq_S1)
{
if(IsDefault(instructions,i)==false)
{
Problems.Add(new Problem(GetResolution("Default is not present in switch statement")));
}
}
}
return Problems;
}
else
{
returnnull;
}
}
staticbool IsDefault(InstructionList instructions,int switchOffset)
{
Instruction instruction =null;
bool DefaultPresent =false;
string SwitchValue =null;
for(int i=switchOffset;i<instructions.Length;i++)
{
instruction = instructions
;
if(instruction.OpCode == OpCode.Br_S)
{
SwitchValue = instruction.Value.ToString();
if(IsDefaultPresent(instructions,i,SwitchValue))
{
DefaultPresent =true;
}
}
}
return DefaultPresent;
}
staticbool IsDefaultPresent(InstructionList instructions,int BrInstruction,string BrValue)
{
Instruction instruction =null;
bool DefaultAvail =false;
string LdstrOffset =null;
for(int i = BrInstruction;i<instructions.Length;i++)
{
instruction = instructions
;
if(instruction.OpCode == OpCode.Ldstr)
{
LdstrOffset = instruction.Offset.ToString(CultureInfo.InvariantCulture);
if(LdstrOffset.Equals(BrValue))
{
if(instruction.Value.Equals("Default"))
{
DefaultAvail =true;
}
}
}
}
return DefaultAvail;
}

