Search code examples
c#fxcopsuppression

FXCop Suppress Warning CA1800 (Unnecessary Casts)


I have the following code:

[SuppressMessage( "Microsoft.Performance", "CA1800:DoNotCastUnnecessarily" )]
private static void SetTestConnectionString( Component table )
{
    if( table is Object1 )
    {
        fn1( (Object1)table );
    }
    // ... a few more if statements for different Classes
}

However, when I run FxCop over this Class/Function it still generates the Warning

warning : CA1800 : Microsoft.Performance : 'table', a parameter, is cast to type 'xxx' multiple times in method 'ccc.SetTestConnectionString(Component)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction.

I know I could refactor this code to remove the warning, however it would make the code less readable. In this instance I would like to suppress this one message on this one function.

What am I doing wrong?


Solution

  • Check if you defined the preprocessor symbol CODE_ANALYSIS in the properties of your project.

    Have a look at: http://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.aspx