3

我有以下代码:

[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
}

但是,当我运行FxCop这个类/函数时,它仍然会生成警告

警告:CA1800:Microsoft.Performance:'table',一个参数,在方法'ccc.SetTestConnectionString(Component)'中多次转换为类型'xxx'。缓存“as”运算符或直接转换的结果,以消除多余的 castclass 指令。

我知道我可以重构此代码以删除警告,但是它会使代码的可读性降低。在这种情况下,我想在这一功能上取消这一消息。

我究竟做错了什么?

4

3 回答 3

5

检查您是否在项目的属性中定义了预处理器符号 CODE_ANALYSIS。

看看:http: //msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.aspx

于 2010-08-19T14:13:08.700 回答
0
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
}
于 2011-02-20T17:29:48.107 回答
0

我怀疑您的项目文件包含的 DebugType 是 none。当设置 DebugType 为 none 时,它​​不会检测到抑制代码。因此,您可以将 DebugType 更改为 full,因为它会正确检测到抑制代码。

<DebugType>full</DebugType>
于 2017-08-11T10:22:49.060 回答