Programmatically Analyse the assembly using FxCop/Code Analysis.
-
Mittwoch, 14. September 2011 10:52
Hi All,
Please let me know how do I programmatically analyse a assembly for FxCop/Code Analysis warnings using C# and get the output.
Highly appriciate if you can give me a hint or reference url .
Thanks
Roshan
Alle Antworten
-
Donnerstag, 15. September 2011 03:33Moderator
You can use FxCopCmd to perform the actual analysis work, what we need to do else is start the FxCopCmd.exe in C# code, and redirect standard input/output.
See also: How do I redirect standard I/O?
Eric Yang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Donnerstag, 15. September 2011 08:54
Thanks you for your valuable reply.
I was able to use FxCopCmd to perform the code analysis. But When I compare the output which generated from FxCopCmd tool against output which generated from Visual Studio Code Analysis, has some discrepancies. After I narrow down the issue by using one Rule dll at a time I was able to find out that Microsoft.Reliability rule dll gives the two difference results for VS Code Analysis and FxCopCmd as follows.
When I run Code Analysis on Visual Studio by selecting only the Microsoft.Reliability Dll in the RuleSet, it gives following 3 warnings,
Warning 1 CA2000 : Microsoft.Reliability : In method 'Connect.ExecuteCodeMetrics(string, string, string)', call System.IDisposable.Dispose on object 'p' before all references to it are out of scope. C:\LasanthaTempProjects\MetricsUpdater\MetricsUpdater\Connect.cs 250 MetricsUpdater
Warning 2 CA2000 : Microsoft.Reliability : In method 'Connect.GetCAWarnings()', call System.IDisposable.Dispose on object 'stream' before all references to it are out of scope. C:\LasanthaTempProjects\MetricsUpdater\MetricsUpdater\Connect.cs 299 MetricsUpdater
Warning 3 CA2000 : Microsoft.Reliability : In method 'Connect.GetCodeMetrics(string)', call System.IDisposable.Dispose on object 'stream' before all references to it are out of scope. C:\LasanthaTempProjects\MetricsUpdater\MetricsUpdater\Connect.cs 272 MetricsUpdater
but when I run FxCopCmd tool by using only the ReliabilityRules.dll it gives 0 warnings.
Please advice me, what could be the reason for this discrepancy ...
Thanks
Roshan
-
Freitag, 16. September 2011 11:18Anybody else have idea about this ??
lasanthaS -
Montag, 19. September 2011 09:06Moderator
Visual Studio might uses different FxCopCmd command line, you can find what command line that Visual Studio used for Code Analysis from OutPut window, and compare with yours:
Here is a sample Visual Studio command line:
Using "CodeAnalysis" task from assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\CodeAnalysis\.\FxCopTask.dll".
Task "CodeAnalysis"
Running Code Analysis...
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe /outputCulture:1033 /out:"bin\Debug\WpfApplication2.exe.CodeAnalysisLog.xml" /file:"bin\Debug\WpfApplication2.exe" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\PresentationCore.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\PresentationFramework.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xaml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\WindowsBase.dll" /directory:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client" /ruleSet:"=MinimumRecommendedRules.ruleset" /rulesetdirectory:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets" /rule:"-C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules" /searchgac /ignoreinvalidtargets /forceoutput /successfile /ignoregeneratedcode /saveMessagesToReport:Active /timeout:120
Microsoft (R) FxCop Command-Line Tool, Version 10.0 (10.0.30319.1) X86
Copyright (C) Microsoft Corporation, All Rights Reserved.
Eric Yang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Montag, 26. September 2011 10:54
Can you please let me know how can I see the above output, Iam using VS2010 and it does not show the above output after I run Code Analysis.
Thanks
Lasantha- Bearbeitet Lasantha S Dienstag, 27. September 2011 03:33
-
Donnerstag, 1. Dezember 2011 11:29Visual Studio 2010 comes with a number of additional rules, the DataFlowRules, these depend on the Phoenix Engine which is only part of the Premium and Ultimate editions of Visual Studio. If you're using the version of FxCop which comes with the .NET SDK, then you won't have these.
My blog: blog.jessehouwing.nl

