Warning: this rules development API is likely to change in a future version of FxCop.
Currently, the locals of a method are stored in a pseudo-instruction in the method InstructionList. We plan to change this in a future version of the tool, the locals should hang directly off the method.
Here's a sample code pattern:
LocalList locals = null; if (method.Instructions.Length > 0) { locals = method.Instructions[0].Value as LocalList; } if (locals != null) |
I'm trying to check local variables too, but I'm using the VS2005 IDE integrated version of FxCop.
The post above works in the stand-alone version 1.35 but doesn't work when using the IDE integrated version.
Can you tell me how I check local variables in the integrated version?
Also is there any way I can debug my rules using the integrated version as obviously the FxCop reference DLLs differ between the two versions (Program Files | FxCop and Program Files | Visual Studio 8 | Team Tools etc) which means that I cannot rely on any debugging done using the stand-alone front end :(
Thanks
The mechanism for checking locals is the same, what's the problem you're seeing?
As long as you stick with 1.35, your rules debugging should be valid between the two. There's always the possibility, though, that we've fixed an issue in 1.35 or made some other change that alters behavior.
To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information.
http://support.microsoft.com/default.aspx?scid=kb;en-us;103861&sd=tech
Sara,
For integration with the Visual Studio version, you will need to change:
foreach (Local local in locals) { } |
to:
for (int i = 0; i < locals.Length; i++) { Local local = locals[i ]; } |
The above code should work with both FxCop 1.35 and Visual Studio versions.
You might to check out the following post by Claudio, who is doing something similar to you: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=299899&SiteID=1
Thanks Michael & David,
As it happens I am actually using VB - not that that should make any difference - and I am already using a For ... Next loop rather than a For Each ... Next loop as shown below:
Dim memberMethod As Method = CType(member, Method) Dim methodLocalsList As LocalList = CType(memberMethod.Instructions(0).Value, LocalList) Dim methodLocal As Local ' check each local in the list For i As Int32 = 0 To methodLocalsList.Length - 1 methodLocal = methodLocalsList.Item(i) ' perform check here Next |
The problem that I'm seeing is that when I run the test in stand-alone FxCop I see the rule failure messages I expected to see, since I've purposefully added local variables to a method which fail the rule to test it, and when I run the exact same rule in the Studio integrated version I don't see any failure messages.
I am performing the rule check within a Check(member) override and I'm specifically looking for a NodeType of Method before attempting to retrieve the locals - if this information helps.
I'll have a look at the debugging information and see if that can get me any further too, thanks :)
PS
Once I have this issue licked, is there any way I can tell if a local is a constant or not since that's another item I need to be able to check.
Um, Has anyone had any further thoughts on this?
It's been a couple of weeks now and I'm still getting the problem...
Thanks
"To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information."
Can you please explain the procedure in detail?
Thanks a lot.
Sara,
Sorry because this thread was already marked as an answer, it wasn't on my radar. Can you send me a repro binary and I'll investigate (see my profile for my email)?
Cheers
David
Is there a way to debug custom rules in vs2005 without using the stand-alone fxcop?
Following is the comment from Michael in this thread -
"To debug your rules within the IDE, you would have to configure a reg key (AEDebug) that specifies the debugger should be launched anytime FxCopCmd.exe is invoked. Here's a link providing some information."
I would appreciate if any one can explain this procedure in detail?
Thanks in advance.
Morning David,
I actually managed to get this rule check mostly going Ok eventually. Looking at my code I'm not sure quite what I was doing wrong to be honest.
Is there a way I can tell if a local variable is defined as a constant though?
I haven't managed to get the rule debugging to work other than by changing my references and using the stand alone version so a bit more info on how to get that going would be really useful if you can provide it.
Thanks,
Sara