문제

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?

도움이 되었습니까?

해결책

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

다른 팁

private static void SetTestConnectionString( Component table )
{
    if( table.GetType() == typeof(Object1) )
    {
        Object1 object1 = (Object1)table;
        fn1( object1 );
    }
    // ... a few more if statements for different Classes
}

I suspect your project file contains the DebugType is none. When set the DebugType is none, it will not detect the suppress code. So you can change the DebugType to full, because it will detect the suppress code properly.

<DebugType>full</DebugType>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top