<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code Analysis Team Blog : FAQ</title><link>http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx</link><description>Tags: FAQ</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>FAQ: How do I prevent FxCop 1.36 from firing warnings against generated code?</title><link>http://blogs.msdn.com/fxcop/archive/2008/02/28/faq-how-do-i-prevent-fxcop-1-36-from-firing-warnings-against-generated-code.aspx</link><pubDate>Thu, 28 Feb 2008 21:39:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7936799</guid><dc:creator>David M. Kean</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/7936799.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=7936799</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=7936799</wfw:comment><description>&lt;p&gt;&lt;em&gt;I've upgraded from FxCop 1.35 to 1.36 and now FxCop has started to fire warnings against typed DataSets and other generated code. How do I turn this off?&lt;/em&gt;&lt;/p&gt; &lt;p&gt;The reason this is occurring is because we changed the way that FxCop analyzed generated code. Whereas previously in 1.35, FxCop would only ignore particular generated code (such as DataSets), FxCop 1.36 will now either ignore no generated code, or all generated code. The former is the default.&lt;/p&gt; &lt;p&gt;To change this behavior and have FxCop skip over generated code:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Using an FxCop project:&lt;/strong&gt;&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Open your FxCop project in FxCop&lt;/li&gt; &lt;li&gt;Choose &lt;strong&gt;Project&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Options&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Spelling &amp;amp; Analysis&lt;/strong&gt;&lt;/li&gt; &lt;li&gt;Check &lt;strong&gt;Suppress analysis results against generated code&lt;/strong&gt;&lt;/li&gt; &lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;&lt;strong&gt;Via the command-line:&lt;/strong&gt;&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Pass the /ignoregeneratedcode switch, for example:&lt;br&gt;&lt;br&gt;&lt;font face="Courier New"&gt;FxCopCmd.exe /file:MyAssembly.dll /out:AnalysisResults.xml /ignoregeneratedcode&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;For more information on FxCop's behavior over generated code, see &lt;a href="http://blogs.msdn.com/fxcop/archive/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute.aspx"&gt;Correct usage of the CompilerGeneratedAttribute and the GeneratedCodeAttribute&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7936799" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: Which Code Analysis rules shipped in which version?</title><link>http://blogs.msdn.com/fxcop/archive/2008/01/07/faq-which-rules-shipped-in-which-version.aspx</link><pubDate>Mon, 07 Jan 2008 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7018062</guid><dc:creator>David M. Kean</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/7018062.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=7018062</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=7018062</wfw:comment><description>&lt;P&gt;In response to &lt;EM&gt;a lot&lt;/EM&gt; of recent requests, we've put together a complete list of rules that shipped in the different versions of Visual Studio Code Analysis and FxCop. Attached is an Excel worksheet providing this information for Visual Studio 2005, Visual Studio 2008, FxCop 1.35 and FxCop 1.36 Beta.&lt;/P&gt;
&lt;P&gt;One of things you'll notice as you read through the list is that we removed some rules from the later versions. There are a few reasons for this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Noise and applicability.&lt;/STRONG&gt; We use feedback from customers, &lt;A href="http://blogs.msdn.com/jensenh/archive/2006/04/05/568947.aspx" mce_href="http://blogs.msdn.com/jensenh/archive/2006/04/05/568947.aspx"&gt;SQM data&lt;/A&gt; (to see which rules users turn off), and input from internal teams (Windows, Office, CLR, etc) to determine the rules that are noisy without adding any perceivable value. There are also rules that are either no longer applicable or can no longer fire. for example, a rule could have been firing on a limitation of the CLR which has since been fixed in later versions.&lt;BR&gt;&lt;BR&gt;
&lt;LI&gt;&lt;STRONG&gt;Merged rules&lt;/STRONG&gt;. Sometimes it makes sense to merge rules that fire on similar things, for example, the analysis in SecureGetObjectDataOverrides was already covered by OverrideLinkDemandsShouldBeIdenticalToBase, so these two rules were merged. Similarly, LongAcronymsShouldBePascalCased, ShortAcronymsShouldBeUppercase and IdentifiersShouldBeCasedCorrectly all fired on the casing of identifiers, and hence were merged in the later.&lt;BR&gt;&lt;BR&gt;
&lt;LI&gt;&lt;STRONG&gt;Analysis engine removed.&lt;/STRONG&gt; In Visual Studio 2008 and FxCop 1.36 we removed one of our analysis engines. This engine was removed for a variety of reasons; it increased analysis time (although the engine encompassed less than 5% our analysis, it took up 50% of our time-to-analyze), indeterministic results (results appearing and disappearing between runs), and bugs found within the engine (and hence the rules that depended on it) required huge architectural changes. We instead decided to invest the resources that we would have spent on fixing the old engine, on a new data flow analysis engine based on &lt;A href="http://research.microsoft.com/phoenix/" mce_href="http://research.microsoft.com/phoenix/"&gt;Phoenix&lt;/A&gt;, which we will ship in a future version of Visual Studio.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;There are also more differences between Visual Studio Code Analysis and FxCop than just the rules - in a future blog post I will cover these in detail.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7018062" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/fxcop/attachment/7018062.ashx" length="55296" type="application/octet-stream" /><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Visual+Studio+2005/default.aspx">Visual Studio 2005</category></item><item><title>FAQ: How do I debug a custom rule?</title><link>http://blogs.msdn.com/fxcop/archive/2007/05/16/faq-how-do-i-debug-a-custom-rule.aspx</link><pubDate>Wed, 16 May 2007 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2650647</guid><dc:creator>David M. Kean</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/2650647.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=2650647</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=2650647</wfw:comment><description>&lt;P&gt;While writing your custom rule,&amp;nbsp;you will likely come across a need to debug it and work out why it's behaving in a particular manner.&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Debugging within FxCop&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Debugging a custom rule within the FxCop UI is actually quite easy. To do so, simply:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open the FxCop project that contains your custom rule (making sure the PDB is alongside the assembly)&lt;/LI&gt;
&lt;LI&gt;Open the solution that contains your custom rule and set a breakpoint in the rule your want to debug&lt;/LI&gt;
&lt;LI&gt;Choose &lt;STRONG&gt;Debug&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Attach to Process&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Under &lt;STRONG&gt;Available Processes&lt;/STRONG&gt;, choose &lt;STRONG&gt;FxCop.exe&lt;/STRONG&gt; and click &lt;STRONG&gt;Attach&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;In FxCop, choose &lt;STRONG&gt;Project&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Analyze&lt;/STRONG&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The debugger should break when it hits the breakpoint.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Debugging within Visual Studio or via command-line&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;However, if you want debug a rule that you've written for Visual Studio Code Analysis, or via the command-line, the procedure is not so obvious:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Open a &lt;STRONG&gt;Visual Studio Command Prompt&lt;/STRONG&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;If debugging a custom rule written for Visual Studio, run the following:&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;BLOCKQUOTE mce_keep="true"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&amp;gt; cd &lt;/FONT&gt;&lt;FONT color=#000000&gt;%VSINSTALLDIR%\Team Tools\Static Analysis Tools\FxCop&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;DIV mce_keep="true"&gt;&lt;/FONT&gt;Otherwise, if debugging a custom rule written for FxCop, run the following:&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&amp;gt; cd %PROGRAMFILES%\Microsoft FxCop 1.35&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Run the following command, replacing &lt;EM&gt;[TargetAssembly]&lt;/EM&gt; and &lt;EM&gt;[RuleAssembly]&lt;/EM&gt; with the assembly you want to analyze and the assembly that contains your custom rule respectively.&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&amp;gt; devenv /debugexe FxCopCmd.exe /file:&lt;EM&gt;[TargetAssembly]&lt;/EM&gt; /rule:&lt;EM&gt;[RuleAssembly]&lt;/EM&gt; /console&lt;/P&gt;&lt;/SPAN&gt;&lt;/BLOCKQUOTE&gt;
&lt;DIV mce_keep="true"&gt;This will open Visual Studio in a state that is ready to debug your custom rule.&lt;BR&gt;&lt;BR&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;Choose &lt;STRONG&gt;File&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Open&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;File&lt;/STRONG&gt; and browse to and open the source file that contains your custom rule&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;Set a breakpoint in the rule&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;Choose &lt;STRONG&gt;Debug&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Start Debugging&lt;/STRONG&gt;&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;The debugger should break when it hits the breakpoint.&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&lt;STRONG&gt;The Modules window is your friend&lt;/STRONG&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;If the breakpoint is not being hit, check to make sure that you actually have symbols for your custom rule loaded:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;While debugging, choose &lt;STRONG&gt;Debug&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Windows&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Modules&lt;/STRONG&gt;&lt;BR&gt;&lt;BR&gt;To do this if you are debugging via &lt;STRONG&gt;/debugexe&lt;/STRONG&gt; switch, as soon as the command window opens to start running FxCop, click &lt;STRONG&gt;PAUSE&lt;/STRONG&gt; on the keyboard&lt;STRONG&gt;.&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;Look for your rule assembly and check under the &lt;STRONG&gt;Symbol Status&lt;/STRONG&gt; column.&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;If this column states anything other &lt;STRONG&gt;Symbols Loaded&lt;/STRONG&gt;, then right-click on the assembly and choose&lt;STRONG&gt; Load Symbols&lt;/STRONG&gt; and browse manually to the PDB.&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2650647" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Custom+Rules/default.aspx">Custom Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: Why is file and line information available for some warnings in FxCop but not for others?</title><link>http://blogs.msdn.com/fxcop/archive/2007/05/12/faq-why-is-file-and-line-information-available-for-some-warnings-in-fxcop-but-not-for-others.aspx</link><pubDate>Sat, 12 May 2007 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1921466</guid><dc:creator>David M. Kean</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1921466.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1921466</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1921466</wfw:comment><description>&lt;P&gt;&lt;EM&gt;It appears that sometimes FxCop displays a link to the source line and file in the Message Details&amp;nbsp;window and sometimes it doesn't. Why this inconsistent behavior?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;There are three usual reasons why this occurs:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;Source lookup is disabled.&lt;/STRONG&gt; To turn source lookup on, choose &lt;STRONG&gt;Project&lt;/STRONG&gt; -&amp;gt; &lt;STRONG&gt;Options&lt;/STRONG&gt; and check &lt;STRONG&gt;Attempt source file lookup&lt;/STRONG&gt;.&lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;The Program Database (PDB) is not present or it is out-of-date.&lt;/STRONG&gt;&amp;nbsp;Starting in Visual Studio 2005, this file is now built by default both in&amp;nbsp;Debug and Release. Make&amp;nbsp;sure it was built at the same time and is alongside the assembly under analysis.&lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;There is no source information for the code element that warning was raised against.&amp;nbsp;&lt;/STRONG&gt;FxCop and Visual Studio Code Analysis both use&amp;nbsp;the information&amp;nbsp;stored in the Program Database (PDB) file&amp;nbsp;to map members back to a particular source file. Unfortunately, because this file was orginally only designed for use by a debugger, it only contains information about actual executable code. This means that FxCop cannot find non-executable code such as namespaces, types, fields, interface methods and abstract methods.&lt;BR&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; Visual Studio Code Analysis does not suffer this problem to extent of FxCop - it&amp;nbsp;falls back to the Visual Studio Code Model to find elements that do not exist in the PDB. It still, however, is unable to find namespaces.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1921466" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: How do I get the SourceContext for a local?</title><link>http://blogs.msdn.com/fxcop/archive/2007/03/27/faq-how-do-i-get-the-sourcecontext-for-a-local.aspx</link><pubDate>Tue, 27 Mar 2007 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1965879</guid><dc:creator>David M. Kean</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1965879.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1965879</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1965879</wfw:comment><description>&lt;P&gt;&lt;EM&gt;I want to fire on the naming of a local, however, whenever I pass&amp;nbsp;the local&amp;nbsp;to the Problem constructor, the source information for the method is always used. How do I get FxCop/Code Analysis&amp;nbsp;to use the source information for the local instead?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Because the declaration of a local is not associated with an executable instruction, a local does not contain any source information within the pdb. However, any usages of the local (such as an assignment) does.&lt;/P&gt;
&lt;P&gt;The following example shows when source information is provided and not provided:&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt; Bar()&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/FONT&gt;&lt;FONT size=2&gt; value1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// Does not have source information&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;value1 = 10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="courier new,courier"&gt;// Has source information&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt; value2 = 20;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="courier new,courier"&gt;// Has source information&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#2b91af size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine(value1); &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// Has source information&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#2b91af size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine(value2); &lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;// Has source information&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" size=2&gt;}&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;As&amp;nbsp;most locals are assigned at their time of declaration (such as in the case of &lt;EM&gt;value2&lt;/EM&gt; in the above example), a simple trick is use the source context of the first usage of the local in place of its declaration. This works in most situations.&lt;/P&gt;
&lt;P&gt;The following example shows this (using the code from &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/04/07/faq-how-do-i-access-the-locals-of-a-method-in-a-custom-rule-michael-fanning-david-kean.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/04/07/faq-how-do-i-access-the-locals-of-a-method-in-a-custom-rule-michael-fanning-david-kean.aspx"&gt;FAQ: How do I access the locals of a method in a custom rules?&lt;/A&gt;):&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#000000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;public&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;override&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;ProblemCollection&lt;/FONT&gt;&lt;FONT size=2&gt; Check(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Member&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt; member)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;Method&lt;/FONT&gt;&lt;FONT size=2&gt; method = member &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;as&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Method&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;if&lt;/FONT&gt;&lt;FONT size=2&gt; (method == &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#000000&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt; (method.Instructions.Length == 0)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;LocalList&lt;/FONT&gt;&lt;FONT size=2&gt; locals = method.Instructions[0].Value &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;as&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;LocalList&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;if&lt;/FONT&gt;&lt;FONT size=2&gt; (locals == &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;for &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt;i = 0; i &amp;lt; locals.Length; i++)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;Local&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; local = locals[i];&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;if&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; (someCondition)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;//&amp;nbsp;Fire on the local&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Resolution&lt;/FONT&gt;&lt;FONT size=2&gt; resolution = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Resolution&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"Fix local {0}"&lt;/FONT&gt;&lt;FONT size=2&gt;, local.Name.Name);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;SourceContext&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt; sourceContext = FindSourceContext(local, method.Instructions);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#2b91af size=2&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;Problem&lt;/FONT&gt;&lt;FONT size=2&gt; problem = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Problem&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" size=2&gt;(resolution, sourceContext);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Problems.Add(problem);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt; Problems;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;static&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SourceContext&lt;/FONT&gt;&lt;FONT size=2&gt; FindSourceContext(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Local&lt;/FONT&gt;&lt;FONT size=2&gt; local, &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;InstructionList&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; instructions)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;foreach&lt;/FONT&gt;&lt;FONT size=2&gt; (&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Instruction&lt;/FONT&gt;&lt;FONT size=2&gt; instruction &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;in&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; instructions)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;if&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; (instruction.Value == local)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;{&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;//&amp;nbsp;Found its first usage&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT size=2&gt; instruction.SourceContext;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;}&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SourceContext&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" size=2&gt;();&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;}&lt;BR&gt;&lt;BR&gt;&lt;FONT face=Arial&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; If the local is only declared and is never assigned to, or its usage is only as the argument for an out parameter, then&amp;nbsp;the source context of the method will be used.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1965879" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Custom+Rules/default.aspx">Custom Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: How do I run FxCop during a post-build event?</title><link>http://blogs.msdn.com/fxcop/archive/2007/02/24/faq-how-do-i-run-fxcop-during-a-post-build-event.aspx</link><pubDate>Sat, 24 Feb 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1738665</guid><dc:creator>David M. Kean</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1738665.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1738665</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1738665</wfw:comment><description>&lt;P&gt;A little known feature of FxCop (in particular FxCopCmd.exe) is its ability to be integrated into the build process within Visual Studio. Although not a replacement for the Code Analysis functionality available within both Visual Studio Team Edition for Developers and Visual Studio Team Suite, it allows you to display FxCop violations within the Error List alongside normal build errors and warnings.&lt;/P&gt;
&lt;P&gt;To set this up is easy:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click on your project and choose &lt;STRONG&gt;Properties&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;For C# projects, in the &lt;STRONG&gt;Project Properties &lt;/STRONG&gt;window, select the &lt;STRONG&gt;Build Events&lt;/STRONG&gt; tab&lt;BR&gt;For Visual Basic projects,&amp;nbsp;in the &lt;STRONG&gt;Project Properties&lt;/STRONG&gt; window, select the &lt;STRONG&gt;Compile &lt;/STRONG&gt;tab, and click &lt;STRONG&gt;Build Events&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;In the &lt;STRONG&gt;Post-build event command-line&lt;/STRONG&gt; text box, enter the following (assuming you installed FxCop to the default location):&lt;BR&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;"%ProgramFiles%\Microsoft FxCop 1.35\FxCopCmd.exe" /file:"$(TargetPath)" /console&lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;&lt;/FONT&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click on your project and choose &lt;STRONG&gt;Build&lt;/STRONG&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Any FxCop violations will now appear as build warnings:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG title="FxCop In ErrorList" style="WIDTH: 891px; HEIGHT: 303px" height=303 alt="FxCop In ErrorList" src="http://blogs.msdn.com/photos/fxcop/images/1748288/original.aspx" width=891 mce_src="http://blogs.msdn.com/photos/fxcop/images/1748288/original.aspx"&gt;&lt;/P&gt;
&lt;P&gt;One thing that you might notice is that although source information is populated in the Error List for warnings raised against properties, events and methods, double-clicking the warning does not cause Visual Studio to jump its location. It turns out this is actually a bug in the MSBuild engine failing to assume the default column of 1 for warnings without column information. This is likely to be fixed in&amp;nbsp;Visual Studio Orcas.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The workaround for this is to simply replace %ProgramFiles%\Microsoft FxCop 1.35\Xml\VSConsoleOutput.xsl (again, assuming you installed to the default location), with the version attached to this post. &lt;/P&gt;
&lt;P&gt;Thanks to David Gardiner for making us aware&amp;nbsp;of &lt;A class="" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1250437&amp;amp;SiteID=1" mce_href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1250437&amp;amp;SiteID=1"&gt;both the bug and workaround&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1738665" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/fxcop/attachment/1738665.ashx" length="1935" type="text/xml" /><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Configuration/default.aspx">Configuration</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: When looking at the names of locals in a custom rule, why do I see strange names such as 'CS$1$0000' and 'VB$1$0000'?</title><link>http://blogs.msdn.com/fxcop/archive/2007/01/26/faq-when-looking-at-the-names-of-locals-in-a-custom-rule-why-do-i-see-strange-names-such-as-cs-1-0000-and-vb-1-0000.aspx</link><pubDate>Fri, 26 Jan 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1450038</guid><dc:creator>David M. Kean</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1450038.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1450038</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1450038</wfw:comment><description>&lt;P&gt;Previously &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/04/07/faq-how-do-i-access-the-locals-of-a-method-in-a-custom-rule-michael-fanning-david-kean.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/04/07/faq-how-do-i-access-the-locals-of-a-method-in-a-custom-rule-michael-fanning-david-kean.aspx"&gt;we showed you how&lt;/A&gt; to access the locals or variables of a method. Once you started to run your rule over an assembly, you might start to notice strange locals with names that start with 'CS$' and 'VB$'. These are compiler generated locals that are outside the control of the user.&lt;/P&gt;
&lt;P&gt;If you are writing a rule to check the names of locals, you will want to skip over these, To do this, simply&amp;nbsp;pass the local to RuleUtilities.IsCompilerGenerated method and check the result.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; This will also skip over locals when&amp;nbsp;source and line&amp;nbsp;information (PDBs)&amp;nbsp;is not alongside the&amp;nbsp;assembly under analysis.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1450038" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Custom+Rules/default.aspx">Custom Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category></item><item><title>FAQ: What exception should I throw instead of the reserved exceptions that DoNotRaiseReservedExceptionTypes warns against?</title><link>http://blogs.msdn.com/fxcop/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx</link><pubDate>Mon, 22 Jan 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1430357</guid><dc:creator>David M. Kean</dc:creator><slash:comments>14</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1430357.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1430357</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1430357</wfw:comment><description>&lt;P&gt;Throwing a general exception type such as &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.exception.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.exception.aspx"&gt;System.Exception&lt;/A&gt; or &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.systemexception.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.systemexception.aspx"&gt;System.SystemException&lt;/A&gt; in a library or Framework forces&amp;nbsp;consumers to catch all exceptions, including unknown exceptions that they do not know how to handle (see &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/faq-why-does-fxcop-warn-against-catch-exception-nick-guerrera.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/faq-why-does-fxcop-warn-against-catch-exception-nick-guerrera.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&lt;/A&gt;&amp;nbsp;for reasons as to why this is bad). &lt;/P&gt;
&lt;P&gt;Instead, either throw a more derived type that already exists in the Framework, or create your own type that derives from Exception.&lt;/P&gt;
&lt;P&gt;The following list examples of when you should throw specific exceptions:&lt;/P&gt;
&lt;P&gt;When validating a parameter (including the &lt;EM&gt;value&lt;/EM&gt; parameter in the set accessor of a property) that:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;is a null reference&amp;nbsp;(&lt;STRONG&gt;Nothing&lt;/STRONG&gt; in Visual Basic)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.argumentnullexception(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.argumentnullexception(vs.80).aspx"&gt;System.ArgumentNullException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;is outside of the allowable range of values (such as&amp;nbsp;an index for a Collection/List)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ArgumentOutOfRangeException(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ArgumentOutOfRangeException(vs.80).aspx"&gt;System.ArgumentOutOfRangeException&lt;/A&gt;&lt;STRONG&gt; &lt;/STRONG&gt;(&lt;STRONG&gt;DO NOT&lt;/STRONG&gt; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.IndexOutOfRangeException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.IndexOutOfRangeException(v.80).aspx"&gt;System.IndexOutOfRangeException&lt;/A&gt;) &lt;/P&gt;
&lt;P&gt;is outside the allowable values for a enum&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.invalidenumargumentexception(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.invalidenumargumentexception(vs.80).aspx"&gt;System.ComponentModel.InvalidEnumArgumentException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;contains a format that not meet the parameter specifications of a method (such as the format string for ToString(String))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.formatexception(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.formatexception(v.80).aspx"&gt;System.FormatException&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;is otherwise invalid (such as an empty string)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ArgumentException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ArgumentException(v.80).aspx"&gt;System.ArgumentException&lt;/A&gt;&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When an operation is invalid for an object's current state:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.invalidoperationexception(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.invalidoperationexception(v.80).aspx"&gt;System.InvalidOperationException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;When an operation is performed on an object that has been disposed:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ObjectDisposedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ObjectDisposedException(v.80).aspx"&gt;System.ObjectDisposedException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;When an operation is&amp;nbsp;not supported (such as&amp;nbsp;in an overridden&amp;nbsp;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.stream.write(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.stream.write(v.80).aspx"&gt;Stream.Write&lt;/A&gt;&amp;nbsp;in a &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.stream(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.stream(VS.80).aspx"&gt;Stream&lt;/A&gt; opened for reading):&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.NotSupportedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.NotSupportedException(v.80).aspx"&gt;System.NotSupportedException&lt;/A&gt;&amp;nbsp;(&lt;STRONG&gt;DO NOT&lt;/STRONG&gt; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.NotImplementedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.NotImplementedException(v.80).aspx"&gt;System.NotImplementedException&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;When a conversion would result in an overflow (such as in a explicit cast operator overload):&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.OverflowException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.OverflowException(v.80).aspx"&gt;System.OverflowException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;For all other situations, consider creating your own type that derives from Exception and throwing that.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt;&amp;nbsp;Exceptions&amp;nbsp;that derive from &lt;STRONG&gt;ArgumentException&lt;/STRONG&gt; (including &lt;STRONG&gt;ArgumentNullException&lt;/STRONG&gt;, &lt;STRONG&gt;ArgumentException&lt;/STRONG&gt;,&lt;STRONG&gt; ArgumentOutOfRangeException &lt;/STRONG&gt;and &lt;STRONG&gt;InvalidEnumArgumentException&lt;/STRONG&gt;),&amp;nbsp;&lt;STRONG&gt;InvalidOperationException&lt;/STRONG&gt; (including &lt;STRONG&gt;ObjectDisposedException&lt;/STRONG&gt;) and &lt;STRONG&gt;NotSupportedException&lt;/STRONG&gt;&amp;nbsp;should only be thrown in situations that are avoidable (such as passing a null argument) and if thrown, would indicate a bug in the calling code. The &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.path(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.path(v.80).aspx"&gt;Path&lt;/A&gt; class &lt;EM&gt;is not&lt;/EM&gt; a good example of this, it incorrect throws&amp;nbsp;ArgumentException&amp;nbsp;to indicate that a&amp;nbsp;path is incorrectly formed, however, it does not expose any methods that can help prevent this from occurring.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1430357" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: How do I fix a violation of MovePInvokesToNativeMethodsClass?</title><link>http://blogs.msdn.com/fxcop/archive/2007/01/14/faq-how-do-i-fix-a-violation-of-movepinvokestonativemethodsclass.aspx</link><pubDate>Sun, 14 Jan 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1429197</guid><dc:creator>David M. Kean</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1429197.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1429197</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1429197</wfw:comment><description>&lt;P&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms182161(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms182161(VS.80).aspx"&gt;MovePInvokesToNativeMethodsClass&lt;/A&gt; fires on&amp;nbsp;P/Invokes&amp;nbsp;(ie methods marked with DllImport) that are not members of&amp;nbsp;one of the following classes;&amp;nbsp;NativeMethods, SafeNativeMethods or UnsafeNativeMethods.&lt;/P&gt;
&lt;P&gt;For example, the following sample fires this warning.&lt;BR&gt;&lt;BR&gt;&lt;FONT size=2&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;&lt;BR&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Runtime.InteropServices;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;internal&amp;nbsp;static&lt;/FONT&gt;&amp;nbsp;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Directory&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;// Violates MovePInvokesToNativeMethodsClass&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; RemoveDirectory(&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; name);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public NotInheritable&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Directory&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Violates MovePInvokesToNativeMethodsClass&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#a31515&gt;"kernal32.dll"&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;, CharSet:=CharSet.Auto)&amp;gt; _&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; RemoveDirectory(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; Name &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;String&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Boolean&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;In the above example, the RemoveDirectory P/Invoke should be moved to an appropriate class that is designed to only hold P/Invokes. 
&lt;P&gt;For most applications, moving P/Invokes to a new class called &lt;EM&gt;NativeMethods&lt;/EM&gt; is typically enough. However, in situations where you are developing reusable libraries for use in other applications then you should also consider defining two other classes called &lt;EM&gt;SafeNativeMethods&lt;/EM&gt; and &lt;EM&gt;UnsafeNativeMethods&lt;/EM&gt;. Both of these classes are similar to the NativeMethods class, however, they are marked with a special attribute called &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.security.suppressunmanagedcodesecurityattribute.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.security.suppressunmanagedcodesecurityattribute.aspx"&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/A&gt;. Applying this attribute causes the runtime to avoid performing a full stack walk to make sure that all callers have the &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission when calling the p/invoke methods containing within these classes. The runtime will check your library for this permission at startup, however, not the assemblies that reference it. This can greatly improve performance when calling unmanaged code and also allows code with limited permissions to call these methods.&lt;/P&gt;
&lt;P&gt;However, using this attribute should not be taken lightly, as implemented incorrectly it can actually have serious security implications.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;NativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;As the NativeMethods class should not be marked with &lt;STRONG&gt;SuppressUnmanagedCodeAttribute&lt;/STRONG&gt;, P/Invokes&amp;nbsp;placed within in it, will require &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission. As most applications run from the local machine and run with FullTrust, this is usually not a problem. However, if you developing reusable libraries, you should instead consider defining&amp;nbsp;a SafeNativeMethods or UnsafeNativeMethods class. &lt;/P&gt;
&lt;P&gt;The following example shows a method Interaction.Beep that wraps the MessageBeep function from user32.dll, the MessageBeep P/Invoke is placed within the NativeMethods class.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.ComponentModel;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Interaction&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// Callers require Unmanaged permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Beep()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// No need to demand a permission as callers of Interaction.Beep&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // will require UnmanagedCode permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if&lt;/FONT&gt; (!&lt;FONT color=#2b91af&gt;NativeMethods&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.MessageBeep(-1))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;throw&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;Win32Exception&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;internal&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;NativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.Auto)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#0000ff&gt;return&lt;/FONT&gt;: &lt;FONT color=#2b91af&gt;MarshalAs&lt;/FONT&gt;(&lt;FONT color=#2b91af&gt;UnmanagedType&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.Bool)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; MessageBeep(&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; uType);&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[Visual Basic]&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.ComponentModel&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Interaction&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers require Unmanaged permission&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Beep()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' No need to demand a permission as callers of Interaction.Beep&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' will require UnmanagedCode permission&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Not&lt;/FONT&gt; NativeMethods.MessageBeep(-1) &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Then&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Throw&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Win32Exception()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;If&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Class&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Friend&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; NativeMethods&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#a31515&gt;"user32.dll"&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;, CharSet:=CharSet.Auto)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; MessageBeep(&lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; uType &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Integer&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &amp;lt;MarshalAs(UnmanagedType.Bool)&amp;gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Boolean&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;In the example above, any assembly that calls NativeMethod.MessageBeep or Interaction.Beep will require &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;SafeNativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;P/Invoke methods that are safe to be exposed to any application and do not have any side effects should be placed in a class called &lt;EM&gt;SafeNativeMethods&lt;/EM&gt; . No permissions need to be demanded and you do not need to pay too much attention to where they are getting called.&lt;/P&gt;
&lt;P&gt;The following example shows a property Environment.TickCount that wraps the GetTickCount function from kernel32.dll.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Security;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Environment&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// Callers do not require UnmanagedCode permission&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; TickCount&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// No need to demand a permission in place of&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;// UnmanagedCode as GetTickCount is considered&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// a safe method&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;SafeNativeMethods&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;.GetTickCount();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;]&lt;BR&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SafeNativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;.Auto, ExactSpelling = &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; GetTickCount();&lt;BR&gt;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Security&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Environment&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers do not require Unmanaged permission&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;ReadOnly&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Property&lt;/FONT&gt; TickCount() &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#0000ff&gt;Get&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' No need to demand a permission in place of&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;' UnmanagedCode as GetTickCount is considered&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;' a safe method&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Return&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; SafeNativeMethods.GetTickCount()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Property&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;SuppressUnmanagedCodeSecurityAttribute()&amp;gt; _&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; SafeNativeMethods&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#a31515&gt;"kernel32.dll"&lt;/FONT&gt;, CharSet:=CharSet.Auto, ExactSpelling:=&lt;FONT color=#0000ff&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; GetTickCount() &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;In the above example, although the assembly that defines Environment.TickCount still requires &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; permission, any callers of it do not.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;STRONG&gt;UnsafeNativeMethods&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;P/Invoke methods that are not safe to be called by anyone and can cause side effects should be placed in a class called &lt;EM&gt;UnsafeNativeMethods&lt;/EM&gt; . These methods should be either stringently checked to make sure that they are not being exposed to the user inadvertently (the rule &lt;EM&gt;Review SuppressUnmanagedCodeSecurity usage&lt;/EM&gt; can help with this) or should have another permission demanded in place of &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; when using them.&lt;/P&gt;
&lt;P&gt;The following example shows a method Cursor.Hide that wraps the ShowCursor function from user32.dll.&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[C#]&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Security;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Security.Permissions;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;Cursor&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// Callers do not require UnmanagedCode permission, however,&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// they do require UIPermissionWindow.AllWindows&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Hide()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// Need to demand an appropriate permission&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;// in place of UnmanagedCode permission as&amp;nbsp;&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;// ShowCursor is not considered a safe method&lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UIPermission(UIPermissionWindow.AllWindows).Demand();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#2b91af&gt;UnsafeNativeMethods&lt;/FONT&gt;.ShowCursor(&lt;FONT color=#0000ff&gt;false&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;SuppressUnmanagedCodeSecurityAttribute&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;]&lt;BR&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#2b91af&gt;UnsafeNativeMethods&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#2b91af&gt;DllImport&lt;/FONT&gt;(&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet = &lt;FONT color=#2b91af&gt;CharSet&lt;/FONT&gt;.Auto, ExactSpelling = &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;extern&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;int&lt;/FONT&gt; ShowCursor([&lt;FONT color=#2b91af&gt;MarshalAs&lt;/FONT&gt;(&lt;FONT color=#2b91af&gt;UnmanagedType&lt;/FONT&gt;.Bool)]&lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; bShow);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;[Visual Basic]&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Runtime.InteropServices&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; System.Security&lt;BR&gt;&lt;FONT color=#0000ff&gt;Imports&lt;/FONT&gt; System.Security.Permissions&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Public&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Cursor&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' Callers do not require Unmanaged permission, however,&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;' they do require UIPermission.AllWindows&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; Hide()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="courier new,courier"&gt;' Need to demand an appropriate permission&amp;nbsp;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier" color=#008000&gt;' in place of UnmanagedCode permission as&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;' ShowCursor is not considered a safe method&amp;nbsp;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim&lt;/FONT&gt; permission &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UIPermission(UIPermissionWindow.AllWindows)&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;permission.Demand()&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;UnsafeNativeMethods.ShowCursor(&lt;FONT color=#0000ff&gt;False&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Sub&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;SuppressUnmanagedCodeSecurityAttribute()&amp;gt; _&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;NotInheritable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Class&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; UnsafeNativeMethods&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;New&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;()&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Sub&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&amp;lt;DllImport(&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#a31515&gt;"user32.dll"&lt;/FONT&gt;, CharSet:=CharSet.Auto, ExactSpelling:=&lt;FONT color=#0000ff&gt;True&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;)&amp;gt; _&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;Friend&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Shared&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Function&lt;/FONT&gt; ShowCursor(&amp;lt;MarshalAs(UnmanagedType.Bool)&amp;gt; &lt;FONT color=#0000ff&gt;ByVal&lt;/FONT&gt; bShow &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;Boolean&lt;/FONT&gt;) &lt;FONT color=#0000ff&gt;As&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;&lt;FONT color=#0000ff&gt;Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;End&lt;/FONT&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;&lt;FONT face="courier new,courier"&gt;Function&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;End&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt; &lt;FONT color=#0000ff&gt;Class&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;In the above example, although the assembly that&amp;nbsp;defines Cursor.Hide still requires &lt;STRONG&gt;UnmanagedCode &lt;/STRONG&gt;permission, any callers of it do not. In place of &lt;STRONG&gt;UnmanagedCode&lt;/STRONG&gt; however, callers will require &lt;STRONG&gt;UIPermission.AllWindows&lt;/STRONG&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1429197" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/SuppressUnmanagedCode/default.aspx">SuppressUnmanagedCode</category></item><item><title>FAQ: What is the GlobalSuppressions.cs/GlobalSuppressions.vb file and why is it needed? Is it possible to change the name of this file? [David Kean]</title><link>http://blogs.msdn.com/fxcop/archive/2006/12/28/FAQ-What-is-the-GlobalSuppressions.cs-GlobalSuppressions.vb-file-and-why-is-it-needed-Is-it-possible-to-change-the-name-of-this-file-David-Kean.aspx</link><pubDate>Thu, 28 Dec 2006 20:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1349800</guid><dc:creator>David M. Kean</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1349800.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1349800</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1349800</wfw:comment><description>&lt;P&gt;&lt;EM&gt;I've noticed that Code Analysis sometimes places suppressions in a file called GlobalSuppression.cs (GlobalSuppressions.vb in Visual Basic). Why this is file needed and it is possible to change its name?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What is this file?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When you right-click on a&amp;nbsp;warning and choose &lt;STRONG&gt;Suppress Message(s)&lt;/STRONG&gt;, Code Analysis checks to see if the warning was raised against an element that&amp;nbsp;exists within source. If it does, such&amp;nbsp;as in the case of a type or a member, then the suppression is applied against the element itself. This is called &lt;EM&gt;In-Source Suppression&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/EM&gt;because the suppression is applied in-source&amp;nbsp;alongside the target&amp;nbsp;of the warning.&lt;/P&gt;
&lt;P&gt;The following sequence shows this:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The user right-clicks on a warning raised against a property and chooses &lt;STRONG&gt;Suppress Message(s)&lt;/STRONG&gt;.&lt;BR&gt;&lt;BR&gt;&lt;IMG src="http://blogs.msdn.com/photos/fxcop/images/1349580/original.aspx" border=0 mce_src="http://blogs.msdn.com/photos/fxcop/images/1349580/original.aspx"&gt;&lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;The suppression (via use of the SuppressMessageAttribute) is applied against the property.&lt;BR&gt;&lt;BR&gt;&lt;IMG src="http://blogs.msdn.com/photos/fxcop/images/1349554/original.aspx" border=0 mce_src="http://blogs.msdn.com/photos/fxcop/images/1349554/original.aspx"&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P mce_keep="true"&gt;If the warning was raised against an element that does not live in source, such as namespaces&lt;STRONG&gt;*&lt;/STRONG&gt;, assemblies and or any other element without source information (ie compiler generated constructors), then Code Analysis places the suppression,&amp;nbsp;by default,&amp;nbsp;in a file called GlobalSuppression.cs (GlobalSuppression.vb in Visual Basic). This is currently called &lt;EM&gt;Module-Level Suppression&lt;/EM&gt; or &lt;EM&gt;Assembly-Level Suppression&lt;/EM&gt; because the suppression is applied at the assembly-level using the &lt;EM&gt;[assembly:]&lt;/EM&gt; declarator, however, as warnings can be raised and suppressed against both a module and assembly at this level, this is confusing terminology&amp;nbsp;and in future versions of Visual Studio this name will likely change.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;*&lt;/STRONG&gt;Although technically namespaces do live within source files, they do not have a representation in the Common Intermediate Language (CIL) and therefore you cannot apply attributes against them.&lt;/P&gt;
&lt;P&gt;The following sequence shows this:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The user right-clicks on a warning raised against an assembly&amp;nbsp;and chooses &lt;STRONG&gt;Suppress Message(s)&lt;/STRONG&gt;.&lt;BR&gt;&lt;BR&gt;&lt;IMG src="http://blogs.msdn.com/photos/fxcop/images/1350285/original.aspx" border=0&gt;&lt;BR&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;The suppression (via use of the SuppressMessageAttribute) is applied against the assembly in GlobalSuppression.cs.&lt;BR&gt;&lt;BR&gt;&lt;IMG src="http://blogs.msdn.com/photos/fxcop/images/1350288/original.aspx" border=0&gt;&lt;A href="http://blogs.msdn.com/photos/fxcop/images/1350288/original.aspx" target=_blank mce_href="http://blogs.msdn.com/photos/fxcop/images/1350288/original.aspx"&gt; &lt;/A&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;As you can see from above, GlobalSuppression.cs/GlobalSuppression.vb exists to store suppressions against elements without source information. If this file does not exist within the project, then Code Analysis will automatically create it. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt; &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/12/21/two-new-features-for-orcas.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/12/21/two-new-features-for-orcas.aspx"&gt;Starting in Orcas&lt;/A&gt;, it will become possible to choose whether to apply a suppression against a element containing source information as an&amp;nbsp;in-source or module-level suppression.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How do I change the name of this file?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;While not very obvious, it is possible to change the name of the Global Suppression file that Code Analysis stores these Module-Level Suppressions in.&lt;/P&gt;
&lt;P mce_keep="true"&gt;To change the name, do the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;Open your project in Visual Studio&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Properties&lt;/STRONG&gt; 
&lt;LI&gt;In the &lt;STRONG&gt;Project Properties &lt;/STRONG&gt;window, choose the &lt;STRONG&gt;Code Analysis&lt;/STRONG&gt; tab 
&lt;LI&gt;In the &lt;STRONG&gt;Code Analysis&lt;/STRONG&gt; tab,&amp;nbsp;choose the rules you want for your minbar 
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Unload Project&lt;/STRONG&gt;, answering &lt;STRONG&gt;Yes&lt;/STRONG&gt; to any prompt to save changes 
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Edit&lt;/STRONG&gt; &lt;/LI&gt;
&lt;LI&gt;Add the following &amp;lt;CodeAnalysisModuleSuppressionsFile&amp;gt; element to the project, replacing &lt;EM&gt;[name] &lt;/EM&gt;with the new name (without path information) of the Global Suppressions file (for example Suppressions.cs):&lt;BR&gt;&lt;BR&gt;
&lt;TABLE class=MsoNormalTable style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #eeeeee; MARGIN: auto; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=1 Table class="MsoNormalTable"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1pt solid; BORDER-TOP: #cccccc 1pt solid; BORDER-LEFT: #cccccc 1pt solid; BORDER-BOTTOM: #cccccc 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;DefaultTargets&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;="&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;Build" &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;="&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;http://schemas.microsoft.com/developer/msbuild/2003"&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;PropertyGroup&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&amp;lt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;CodeAnalysisModuleSuppressionsFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;EM&gt;[name]&lt;/EM&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;CodeAnalysisModuleSuppressionsFile&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;FONT color=#000000&gt;[...]&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#0000ff size=2&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;lt;/&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P mce_keep="true"&gt;If you want all your projects to use the same name,&amp;nbsp;see the&amp;nbsp;following: &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/11/16/faq-how-do-i-share-managed-code-analysis-rule-settings-over-multiple-projects-david-kean.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/11/16/faq-how-do-i-share-managed-code-analysis-rule-settings-over-multiple-projects-david-kean.aspx"&gt;FAQ: How do I share Managed Code Analysis rule settings over multiple projects?&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1349800" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Configuration/default.aspx">Configuration</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Suppressions/default.aspx">Suppressions</category></item><item><title>FAQ: How do I share Managed Code Analysis rule settings over multiple projects? [David Kean]</title><link>http://blogs.msdn.com/fxcop/archive/2006/11/16/faq-how-do-i-share-managed-code-analysis-rule-settings-over-multiple-projects-david-kean.aspx</link><pubDate>Thu, 16 Nov 2006 20:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1087747</guid><dc:creator>David M. Kean</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1087747.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1087747</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1087747</wfw:comment><description>&lt;DIV&gt;
&lt;P&gt;If your team has a minbar of Managed Code Analysis rules that must be explicitly fixed or suppressed, it is possible to share the&amp;nbsp;Managed Code Analysis rule settings&amp;nbsp;over multiple MSBuild projects (.csproj, .vbproj).&lt;/P&gt;
&lt;P&gt;To share the minbar between multiple projects, do the following:&lt;/P&gt;
&lt;DIV&gt;
&lt;OL&gt;
&lt;LI&gt;Using Visual Studio, create a new empty project&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Properties&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;In the &lt;STRONG&gt;Project Properties &lt;/STRONG&gt;window, choose the &lt;STRONG&gt;Code Analysis&lt;/STRONG&gt; tab&lt;/LI&gt;
&lt;LI&gt;In the &lt;STRONG&gt;Code Analysis&lt;/STRONG&gt; tab,&amp;nbsp;choose the rules you want for your minbar&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Unload Project&lt;/STRONG&gt;, answering &lt;STRONG&gt;Yes&lt;/STRONG&gt; to any prompt to save changes&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click the project and choose &lt;STRONG&gt;Edit&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Search for the &lt;STRONG&gt;&amp;lt;CodeAnalysisRules&amp;gt;&lt;/STRONG&gt; element and copy the text within it&lt;/LI&gt;
&lt;LI&gt;Create an empty text file called &lt;EM&gt;[team].CodeAnalysis.Rules.targets&lt;/EM&gt;, replacing &lt;EM&gt;[team]&lt;/EM&gt; with the name of your team&lt;/LI&gt;
&lt;LI&gt;In the text file enter the following, replacing &lt;EM&gt;[rulesettings]&lt;/EM&gt; with the text copied above in Step 6:&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;
&lt;TABLE class=MsoNormalTable style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #eeeeee; MARGIN: auto; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=1 Table class="MsoNormalTable"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1pt solid; BORDER-TOP: #cccccc 1pt solid; BORDER-LEFT: #cccccc 1pt solid; BORDER-BOTTOM: #cccccc 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;=&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;"&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;http://schemas.microsoft.com/developer/msbuild/2003&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;"&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;PropertyGroup&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;CodeAnalysisRules&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;$(CodeAnalysisRules);&lt;EM&gt;[rulesettings]&amp;lt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;CodeAnalysisRules&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&amp;nbsp; &lt;BR&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;PropertyGroup&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P mce_keep="true"&gt;If you do not want developers to be able to turn on extra rules over and above the minbar, do not&amp;nbsp;add the '&lt;EM&gt;$(CodeAnalysisRules);&lt;/EM&gt;' text. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;Using Visual Studio, open your projects&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click a project and choose &lt;STRONG&gt;Unload Project&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;In &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, right-click&amp;nbsp;the unloaded project and choose &lt;STRONG&gt;Edit&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Add the following &amp;lt;Import&amp;gt; element to the project, replacing &lt;EM&gt;[path] &lt;/EM&gt;with the path of the targets file created above. This needs to be the last line (just above the closing &amp;lt;/Project&amp;gt; element) to ensure that settings within the project do not override the team settings:&lt;BR&gt;&lt;BR&gt;
&lt;TABLE class=MsoNormalTable style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BACKGROUND: #eeeeee; MARGIN: auto; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 border=1 Table class="MsoNormalTable"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: #cccccc 1pt solid; BORDER-TOP: #cccccc 1pt solid; BORDER-LEFT: #cccccc 1pt solid; BORDER-BOTTOM: #cccccc 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;DefaultTargets&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;="&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;Build" &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;="&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;http://schemas.microsoft.com/developer/msbuild/2003"&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;FONT color=#000000&gt;[...]&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;STRONG&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Import&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;=&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'"&gt;"&lt;SPAN style="COLOR: blue"&gt;&lt;EM&gt;[path]&lt;/EM&gt;&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;FONT color=#0000ff size=2&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: maroon; FONT-FAMILY: 'Courier New'"&gt;Project&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;BR&gt;&lt;/LI&gt;
&lt;LI&gt;Repeat from Step 11 for each project you want to share the rule settings with&lt;/LI&gt;&lt;/OL&gt;&lt;/DIV&gt;
&lt;P&gt;Any projects that imported the common rule settings via Step 9, should now reflect these settings in the Code Analysis properties pane. You can also share any&amp;nbsp;MSBuild properties using similar steps.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please Note:&lt;/STRONG&gt;&amp;nbsp;In order to avoid&amp;nbsp;Visual Studio prompting about unsafe imported MSBuild projects, you need to&amp;nbsp;explicitly trust the &lt;EM&gt;[team].CodeAnalysis.Rules.targets&lt;/EM&gt; file. To do this, use the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Run regedit&lt;/LI&gt;
&lt;LI&gt;Navigate to &lt;STRONG&gt;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\MSBuild\SafeImports&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Add a new string value called &lt;STRONG&gt;[team].CodeAnalysis.Rules.targets&lt;/STRONG&gt; and set its value to the full path of the targets file.&lt;/LI&gt;&lt;/OL&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1087747" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Configuration/default.aspx">Configuration</category></item><item><title>FAQ: Why is FxCop 1.32 not available for download? [David Kean]</title><link>http://blogs.msdn.com/fxcop/archive/2006/07/11/faq-why-is-fxcop-1-32-not-available-for-download-david-kean.aspx</link><pubDate>Wed, 12 Jul 2006 05:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:662993</guid><dc:creator>David M. Kean</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/662993.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=662993</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=662993</wfw:comment><description>&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&amp;nbsp;While SharePoint Portal Server 2003 will not run side-by-side with .NET 2.0, SharePoint Portal Server 2003 SP2 will. For more information, see: &lt;A href="http://support.microsoft.com/kb/907763"&gt;http://support.microsoft.com/kb/907763&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;We have had a lot of users contacting us since we released &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/23/644638.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/23/644638.aspx"&gt;FxCop 1.35&lt;/A&gt;, asking for a link to be able to download a copy of FxCop 1.32. There&amp;nbsp;appears to be some misconceptions with&amp;nbsp;FxCop 1.35&amp;nbsp;that we thought we would try to clear up in this post.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Misconception #1: FxCop only analyzes .NET 2.0 assemblies.&lt;BR&gt;&lt;/STRONG&gt;Although FxCop 1.35 does require the .&lt;A href="http://www.microsoft.com/downloads/details.aspx?familyid=0856eacb-4362-4b0d-8edd-aab15c5e04f5&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?familyid=0856eacb-4362-4b0d-8edd-aab15c5e04f5&amp;amp;displaylang=en"&gt;NET Framework 2.0&lt;/A&gt; to be installed, it&amp;nbsp;can analyze assemblies compiled against all versions of .NET, including .NET 1.0 and .NET 1.1.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Misconception #2: .NET 1.x and .NET 2.0 cannot be installed on the same system.&lt;BR&gt;&lt;/STRONG&gt;Both .NET 1.x&amp;nbsp;and .NET&amp;nbsp;2.0 and&amp;nbsp;Visual Studio 2002/2003 and Visual Studio 2005&amp;nbsp;can be happily run side-by-side&amp;nbsp;on a system&amp;nbsp;without affecting each other. If&amp;nbsp;you do find any bugs with these configurations, then &lt;A href="https://connect.microsoft.com/feedback/default.aspx?SiteID=210" mce_href="https://connect.microsoft.com/feedback/default.aspx?SiteID=210"&gt;we need to know about it&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Misconception #3: FxCop 1.35 requires Visual Studio 2005.&lt;/STRONG&gt;&lt;BR&gt;FxCop 1.35 does not and never has, required any version of Visual Studio to be installed.&lt;/P&gt;
&lt;P&gt;There are also a number of other reasons why we have not made FxCop 1.32 available:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Bugs.&lt;/STRONG&gt; 1.32 is over a year old; since then, there have been 150+ bug fixes, (including&amp;nbsp;all in Visual Studio 2005 Code Analysis), that all made it into 1.35. 
&lt;LI&gt;&lt;STRONG&gt;Security.&lt;/STRONG&gt; As part of the bugs fixes, we fixed some of the security rules that were giving completely wrong analysis. Because of this, you do&amp;nbsp;not want to base any security&amp;nbsp;decisions based on the analysis that 1.32 gives. 
&lt;LI&gt;&lt;STRONG&gt;Noise Reduction.&lt;/STRONG&gt; For 1.35, we have made significant changes to the amount of noise that FxCop fires&amp;nbsp;against code, including generated and C++/CLI. 
&lt;LI&gt;&lt;STRONG&gt;Support.&lt;/STRONG&gt; FxCop is, officially, an unsupported tool, however, individual members of the Code Analysis team spend a significant amount of work (and personal) time in the &lt;A href="http://forums.microsoft.com/msdn/default.aspx?siteid=1" mce_href="http://forums.microsoft.com/msdn/default.aspx?siteid=1"&gt;FxCop&lt;/A&gt; forums answering questions.&amp;nbsp;Attempting to support old versions of FxCop means that we spend less time working on newer features and better analysis for our customers.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If the above doesn't convince you to move FxCop 1.35, there are also a number of new features such as 5 new rules, in source suppression and GAC support.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=662993" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Releases/default.aspx">Releases</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 3 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Tue, 20 Jun 2006 07:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:638453</guid><dc:creator>nicholg</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/638453.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=638453</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=638453</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the third installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;BR&gt;&lt;/EM&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;/A&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;I said from the &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx"&gt;beginning&lt;/A&gt; that this issue is controversial, and some of your &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/17/635835.aspx#637002" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/635835.aspx#637002"&gt;feedback&lt;/A&gt; certainly confirms that. I want to make it clear that I respect everyone’s right to disagree with me, to exclude or disable the FxCop warning, and to implement a less rigid exception policy in their &lt;I style="mso-bidi-font-style: normal"&gt;application code &lt;/I&gt;as they see fit. Furthermore, all of the opinions I’ve expressed are entirely my own and they represent a stricter interpretation of the Framework Design Guidelines&amp;nbsp;than absolutely required. I believe strongly that the approach that I’ve outlined helps to find and fix defects sooner during development and testing and therefore helps to deliver more robust production software. Nevertheless, the choice to adopt a different strategy for your application is entirely yours. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;On the other hand, as I mentioned in the previous post, this FxCop rule&amp;nbsp;is of paramount importance for &lt;I style="mso-bidi-font-style: normal"&gt;class library code&lt;/I&gt;. When class libraries call back to application code (via virtual methods, interface methods, events, or delegates), it is disastrous if they swallow or disguise the arbitrary exceptions that may be raised. (To see just how aggravating this can be for library consumers, witness the frustration with the WebBrowser control that readers expressed earlier.) Furthermore, class library code cannot make any assumptions about the implications of arbitrary exceptions for application code further up the call stack, so it must let them go so that the application can decide how to handle them. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;With that said, let me now address the most recent comments with more of my own personal opinions. :)&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“… I hope you do realize that a number of the recommendations you have given to people, myself included, basically amount to ‘let your app crash, your customers and business partners be damned.’ …" &lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;No, I most certainly do not realize that, because it’s simply not true. Applications which catch only the precise exceptions that they are prepared to handle gracefully are easier to develop and maintain over the long run. When this strategy is combined with strong testing, the application will ship with fewer defects and there will be very few unhandled exceptions in the wild. The truth is that once an unexpected exception has been raised in your application, customers and partners are likely to suffer one way or another. The impact of corrupt program state can range from security vulnerability to plain incorrect output, which can end up costing customers more time and money than dealing with a crash. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Please don’t misconstrue this to mean that it’s ok to write sloppy software which crashes regularly in common cases. Clearly that’s unacceptable and I never said otherwise. However, the right way to avoid this fate is to design your software carefully, implement it methodically, and test it thoroughly. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“I think all of your readers know full well that applications need to be designed to fail fast and do so clearly in order to ease the diagnosis and correction of software defects.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I’m glad that we agree that applications should be designed to “fail fast.” From my point of view, once an exception is raised in your application and you haven’t decided exactly how to handle it on its own terms, then the application has already “failed” and process termination represents the “fast” part of the equation. :)&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: “&lt;I style="mso-bidi-font-style: normal"&gt;We do this, however, by modularizing software and testing it at an appropriate level of granularity, not by letting the whole app fall down in production.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I do agree that it’s possible to sandbox components (for example, by using separate app-domains and ensuring that all shared data is immutable) such that the failure of a single component does not imply failure for the entire process. This then becomes, as you say, a question of granularity. You can think of each component as a mini-process, and then apply all of my arguments on a component-by-component basis. That is to say that when an isolated component throws an unexpected exception, its state must be discarded and it must be reloaded before it can reliably be consumed again. If your application is hardened in this way, then you would in fact&amp;nbsp;need to catch (Exception) in all of the locations that you mentioned in your first comment in order&amp;nbsp;to save the host process from termination. I missed that in my original reply and I apologize.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;However, I’d like to point out that it’s difficult and costly to implement this strategy correctly and I still&amp;nbsp;maintain that the approach I've been recommending is perfectly viable for most applications.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“Your recommendations will only work well for the people replying to your posts if you are interested in personally supporting their products when deployed in the field.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Given the choice between an application that abuses catch (Exception) and another which catches only the exceptions&amp;nbsp;that it can handle gracefully, I'd choose to support the second application in a heart-beat.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“Their customers and partners don't care about what FxCop might have recommended to the developer.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Not directly, but they do care about software quality, reliability, and overall correctness. Let’s take two familiar examples: Microsoft Excel and the C# compiler. I don’t want Excel to crash, but it would be much worse if it ever produced an incorrect value in a spreadsheet that I use to manage my personal finances. Similarly, I prefer to see the compiler ICE (Internal Compiler Error) in favor of emitting bad code. Of course, in an ideal world, Excel would never crash and the compiler would never ICE, but I’ve seen both happen and I’m sure it was in my best interest even if it upset me at the time. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“I think you have to realise that exception handling is not just for the developer's benefit. The application is there to serve the user, not the developer, so simply bombing out because you can't display a new form is going to require the user to restart the application more than is really required.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Yes, the application is there to serve the user, not the developer and I couldn’t agree more. In that situation, you have to ask yourself why you couldn’t display the form.&amp;nbsp;If the only reason you can provide is that your catch (Exception) block was hit, then you really have no idea whether it’s in the best interest of the user to continue executing. For example, what if one of your assemblies just failed to load, or you’ve run out of memory, or the root cause is actually a corruption in shared data? What will go wrong next? On the other hand, if you catch (IOException) while trying to read the CSV file&amp;nbsp;that will ultimately be used to populate the new form, then you know beyond a shadow of a doubt that the correct application behavior is to inform the user that the file could not be opened and allow them to continue to use the application which remains in a consistent state.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“In a previous project I worked on, we had a lot of places where exceptions were caught and error reports were sent back to the dev team. Therefore, the exceptions weren't just disappearing quietly, they were known to the developers. &lt;BR&gt;&lt;BR&gt;In many circumstances, the application would then continue. This is because a lot of our handling of exceptions would result in the form the exception came from closing or not appearing. Basically that element of the workflow did not occur. For almost all cases, this left the application in a state that allowed it to continue without problems. Of course, it wasn't 100% perfect. We did have a threshold of a certain number of exception reports in a certain amount of time would cause the handler to exit the app, though.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Both exception logging and using an exception count threshold are popular approaches for applications which catch all exceptions in places. In fact, this is actually exactly how the current version of FxCop works. Our experience with FxCop’s current exception handling policy has not been positive at all and so I would not suggest this approach universally. Nevertheless, as the application developer, it’s up to you to choose the appropriate policy.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“Especially when the environment is such that you cannot deploy a fix at a day's notice, having the users restarting the application frequently when you could provide better is basically developing to suit the developers instead of the users.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If it turns out that your application is crashing “frequently” enough that users have to restart it all the time, then it wasn’t ready to ship in the first place. By the time you’re ready to ship, you should have uncovered nearly all of the exceptions that you need to handle or prevent, and those that remain should be extremely uncommon. Besides careful testing on your end during the development cycle, it’s important to provide regular preview releases for your customers and partners to pound on. During that time, it will be invaluable that exception-related bugs are easy to diagnose and fix quickly. By the time you ship, you will have software which neither crashes nor swallows bugs!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“Of course, it can make diagnosing problems more difficult, but the role of the software is to make the user's job easier, not the developer's. Sometimes the developer just has to work a bit harder because otherwise the work goes on the user's plate.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I never meant to imply that ease of diagnosis is an end unto itself. It is a means through which you can deliver better software with fewer defects, which most definitely serves to make the user’s life easier. Indeed, the developer often does have to work harder: uncovering the specific exceptions that need to be handled and the additional code that needs to be in place to prevent the others is often harder than slapping in an additional catch (Exception) block, but it’s the right thing to do…&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=638453" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 2 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Sun, 18 Jun 2006 08:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:635835</guid><dc:creator>nicholg</dc:creator><slash:comments>28</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/635835.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=635835</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=635835</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the second installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;BR&gt;&lt;/EM&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;/A&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;On Wednesday, I explained why catch (Exception) is a &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx"&gt;bad idea&lt;/A&gt;, and many of you replied with interesting comments. Since it was too cumbersome to write everything I had to say in the comments section, I’ve replied to your comments below. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;K: &lt;I style="mso-bidi-font-style: normal"&gt;“Do you have a list of the 'unchecked' exceptions?”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;No. I don’t have a definitive list of the exceptions which should always be prevented and never caught. The best advice I can offer here is to follow the API documentation on a case-by-case basis. Don’t immediately assume that you need catch blocks for each of the exceptions that are listed in the documentation for a particular API. Instead, for each exception, ask you yourself if there’s a simple way to prevent it. If there is, then put in the preventative code and leave out the catch block. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;This can be tough to discern in some circumstances. For example, it may seem that a call to File.Exists can prevent FileNotFoundException, but there’s a race condition where the operation will still fail if the file is deleted in between the time File.Exists returns true and the call is initiated. Also, there are some methods in the .NET Framework which misuse exception types. For example, in an ideal world, you should never catch ArgumentException, but Enum.Parse throws it to indicate that the input does not represent one of the enum values. Since there’s no way to prevent that short of rewriting Enum.Parse, you have to consider ArgumentException ‘checked’ in that specific case. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you’re unsure about a specific API, try asking for help on &lt;A href="http://forums.microsoft.com/" mce_href="http://forums.microsoft.com"&gt;http://forums.microsoft.com&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Gill: &lt;I style="mso-bidi-font-style: normal"&gt;“… there is one issue that troubles me: what if my program handles 10 specific exceptions I defined in the same way (when all these exceptions hint at different problems that are related), in this case I would want some catch blocks to perform the same code. If these exceptions don't share a common base, there is no way to make all of them perform the same code short of using the same lines (or method call) in each catch block.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I don’t think there are that many methods out there which throw 10 unrelated and unpreventable exceptions, but I can relate to your frustration with API which throw unrelated exceptions to indicate related failures. Refactoring the shared handler code in to a helper method is the simplest way to deal with the issue. If you find yourself catching the same set of exceptions from the same method in more than one place, consider refactoring out the entire try/catch/catch/… series in to a helper method of its own.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Gill: “&lt;I style="mso-bidi-font-style: normal"&gt;Furthermore, if all developers worked correctly and used this guideline my code might be facing unexpected exceptions from 3rd party code bugs. If this 3rd party isn't available to fix the bug, my program can fail at unexpected times.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I don’t follow the reasoning here. If you hit a bug in 3&lt;SUP&gt;rd&lt;/SUP&gt; party code, then catch (Exception) is just going to make things worse. You should treat this unhandled exception no different from any other. It may turn out that you can work around the problem by using the API differently or adding a catch handler for the specific exception type that was raised if it turns out to be recoverable. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Richard: &lt;I style="mso-bidi-font-style: normal"&gt;“Of course, it would help if the Exception class was abstract, and the Framework never threw a general Exception! For example, try calling System.Drawing.ColorTranslator.FromHtml("#aaax")”&lt;/I&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Youch! You’re right; it’s absolutely terrible to throw an instance of System.Exception and FxCop warns against that as well. I will do my best to argue for reopening and fixing this bug!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Anonymous: &lt;I style="mso-bidi-font-style: normal"&gt;“If my program has a feature to auto-save unsaved work every 10 minutes, and the auto-save procedure (which may use a third-party library) throws an unchecked exception, are you suggesting that the program should simply crash out, because this exception must not be handled?”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Yes, that is in fact exactly what I’m suggesting. A bug is a bug is a bug. It doesn’t matter if it’s in your code or 3&lt;SUP&gt;rd&lt;/SUP&gt; party code, catch (Exception) will just make it harder to find and fix. Having an auto-save feature is a great way to mitigate the risk of data loss. However, if the auto-save code itself is buggy, then all bets are off.&amp;nbsp;It’s sort of like discovering that your parachute won’t open…&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeff: &lt;I style="mso-bidi-font-style: normal"&gt;“Well here is my slight problem with this. The best example I can give you as to why you absolutely have [to] Catch(Exception ex) to do this is a bug I reported back in Beta 2 … The Browser Control …”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Chris: &lt;I style="mso-bidi-font-style: normal"&gt;“… the WebBrowser control will silently eat the NullReferenceException. I want to know when if my code has a bug, but the WebBrowser hides my bugs&lt;/I&gt;.”&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;That’s awful! I will talk to the owners of the Web browser control about getting this fixed. One of the things that I failed to mention in my original post is that it’s much worse to swallow exceptions in library code than in application code. If you’re developing a reusable library, this rule (like many others in FxCop) is doubly important!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Pop Catalin Sever:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;I style="mso-bidi-font-style: normal"&gt;“Let the entire application crash? Are you kidding? That's really unacceptable. And btw if you notify the user that something bad has happened in most cases he will help out recover the application or some data. If you just let the application close I think that very likely you're going to be in big trouble.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Let the application continue to run in&amp;nbsp;an indeterminate state and carelessly corrupt data? Are you kidding? ;) &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;While I agree that the user should be notified that something has gone wrong, I don’t agree that the application should continue running afterwards. Instead, a friendly dialog should allow the user to send an error report before the application closes. If data loss is a potential issue in your application, then implement an auto-save feature. It’s worth noting that this is exactly the approach that’s used&amp;nbsp;in Microsoft Office.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Beginning in .NET 2.0, you don’t need to implement the crash dialog box yourself. If you let the exception go unhandled, a default dialog box will be displayed that allows the user to send an error report to Microsoft. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Note that for Windows Forms applications, you need to call Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException) to make this work.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you prefer, you can hook the AppDomain.UnhandledException event and display your own dialog that allows users to send a bug report directly to you.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;EM&gt;“Here are another three cases where you absolutely must catch Exception (and at the very least provide for adequate logging, even if you then rethrow): &lt;BR style="mso-special-character: line-break"&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;I style="mso-bidi-font-style: normal"&gt;1. In every method you spin up in a new thread. &lt;BR&gt;2. In every method called arbitrarily on threads you don't control (ie. WebMethods, methods exposed via remoting, delegates passed across remoting, windows service control overrides, etc.) &lt;BR&gt;3. &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;Main&lt;/st1:place&gt; &lt;BR&gt;&lt;BR&gt;Seeing a pattern here? :)”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;It sounds like you got burned by the default exception handling policy for secondary threads in .NET 1.0 and 1.1. Thankfully, the situation is vastly improved in .NET 2.0. See&amp;nbsp;&lt;A href="http://msdn2.microsoft.com/en-us/library/ms228965.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms228965.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms228965.aspx&lt;/A&gt;. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you can’t move to 2.0 just yet, then a better approach than sprinkling catch (Exception) in all of these places is to hook AppDomain.UnhandledException (and Application.ThreadException for Windows Forms applications). In your handler, you can throw up a dialog like the default dialog in .NET 2.0 and call Environment.Exit afterwards.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=635835" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 1 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Thu, 15 Jun 2006 09:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:631923</guid><dc:creator>nicholg</dc:creator><slash:comments>26</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/631923.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=631923</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=631923</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the first&amp;nbsp;installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;/EM&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This question comes up a lot, and I think there’s a lot of confusion and controversy about the rule. Many people seem to think that it’s noisy, annoying, and not worth their time. I find that very unfortunate because following its guidance can really help you build better software. Here’s my attempt to convince the Nay-Sayers that catch(Exception) is almost always a terrible idea.&lt;/P&gt;
&lt;P&gt;I’ve heard a lot of arguments against DoNotCatchGeneralExceptionTypes, but they generally boil down to one or both of the following:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;“If I call an API which doesn’t document which exceptions it can throw, then I have no choice but to catch all possible exceptions!” 
&lt;LI&gt;“My application needs to be robust; it can’t crash just because of an exception that I forgot to handle!”&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Both arguments are ill-conceived because they presume that you can handle an exception even if you don’t know what failure it represents or how it impacts the code that will execute next. There’s an implicit assumption in both arguments that the mere act of catching an exception is equivalent to handling it appropriately, which simply isn’t true. &lt;/P&gt;
&lt;P&gt;For the sake of the discussion to follow, exceptions can be divided in to two general categories: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Those that signal bugs (i.e. API misuse), or catastrophic system failures&lt;BR&gt;(e.g. ArgumentException, NullReferenceException, ExecutionEngineException.) 
&lt;LI&gt;Those that signal unpreventable yet recoverable error conditions.&lt;BR&gt;(e.g. FileNotFoundException, SocketException.)&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;In languages like Java, which have a notion of checked exceptions, only exceptions of the second kind are eligible to be checked while exceptions of the first kind must be left unchecked. On that basis and for lack of better terminology, I will hereafter refer to exceptions in the first bucket as ‘unchecked’ and exceptions in the second bucket as ‘checked’. (Note the quotes.) I am using the terms loosely to mean that if exceptions were checked in .NET, then those in the first bucket would remain unchecked while those in the second bucket could become checked.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Independent of where you stand on the checked exception debate, this distinction is very important to hold in your mind when writing exception handling code. In particular, do you really ever want to catch the ‘unchecked’ exceptions at all? Probably not! Instead, you should prevent them from occurring in the first place. For example, check if a variable is null before dereferencing it, don’t catch (NullReferenceException).&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;If a bug in your code manifests itself as an exception (and most of the ‘unchecked’ exceptions are just that), then it’s best to let the application terminate right at the point of failure. When the bug is discovered, you’ll get exactly the diagnostic information you need to fix it quickly, add the appropriate regression tests, and move on. On the other hand, debugging a system which aggressively swallows general exceptions feels like you’re one of the police investigators in “CSI: Miami”. The culprit has left the crime scene and you have to work backwards from the subtle clues in your program’s incorrect behavior.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;And that’s the principal reason to avoid catch (Exception): it hides bugs in your code. These bugs are far more costly to resolve and since they can go undetected, the quality of your software suffers. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Some readers might be thinking that it would be helpful if the .NET Framework exception class hierarchy reflected the ‘checked’ vs. ‘unchecked’ nature of exceptions. In fact, it’s rather seductive to imagine a base class, say System.CheckedException, for all of the ‘checked’ exceptions. The argument follows that you could then replace catch (Exception) with catch (CheckedException)… and the problem would be solved since you no longer catch those insidious ‘unchecked’ exceptions. Unfortunately, this approach would be just as flawed. FxCop would still warn against catch (CheckedException) because it’s still far too general. By definition, ‘checked’ exceptions are unpreventable and must therefore be caught. Nevertheless, catching them is the easy part; the hard part is deciding what to do next, and that decision is purely a function of the specific exception type and it’s meaning in the given context. For example, to recover from FileNotFoundException for your configuration file, you might use a default configuration, and to recover from a SocketException, you might fall back to a local store until the network becomes available again. However, if you don’t know what to do about it, then let it go.&lt;BR&gt;&lt;BR&gt;Returning to the first argument against this rule, please don’t get me wrong, I definitely empathize with the need for accurate exception information in API documentation. However, in the absence of such documentation, the safest approach is to treat all API as &lt;BR&gt;“innocent until proven guilty.” Wait until you have hard evidence that a particular exception can be raised before you attempt to handle it. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Also, be sure to test your exception handling code. For example, delete a file out from underneath your application while it’s running and see how it deals with FileNotFoundException. It’s simply not sufficient to write optimistic and untested code, just because the exception is mentioned in the API documentation. I can’t tell you how many times I’ve seen untested catch handlers fail to work as expected. The classic example is a formatting exception raised in the error message that’s never provoked from a test. Spurious and unnecessary catch (Exception) blocks can completely torpedo your code coverage! (Remember: if it’s not tested, it’s broken.)&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;One important thing to note is that if you follow our advice and catch fewer exceptions, then exceptions will naturally travel further up the call stack. Sometimes they’ll terminate the application, but other times someone further up the call stack will know what to do and your only job is to clean up after yourself in finally blocks as the stack unwinds.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To summarize, here are some important things to consider when dealing with exceptions in managed code: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Do not catch preventable (‘unchecked’) exceptions. &lt;BR&gt;Only catch the specific unpreventable (‘checked’) exceptions that you are certain can be raised and that you know how to handle. 
&lt;LI&gt;Test your exception handling code. 
&lt;LI&gt;Use finally blocks liberally for clean-up code to keep your state as consistent as possible in case an exception that you cannot handle needs to be handled further up the stack.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;And finally, I’d like to point out that our team has learned this lesson the hard way. There are actually far too many catch (Exception) blocks in FxCop itself and it has caused us a great deal of grief. We’re working on removing them and I’m confident it will help us to deliver better software!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=631923" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item></channel></rss>