【问题标题】:FXCop Suppress Warning CA1800 (Unnecessary Casts)FXCop 抑制警告 CA1800(不必要的演员表)
【发布时间】:2011-04-01 03:48:49
【问题描述】:

我有以下代码:

[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',一个参数,是 在方法中多次转换为“xxx”类型 'ccc.SetTestConnectionString(组件)'。缓存'as'的结果 运算符或直接强制转换,以消除多余的强制转换类 说明。

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

我做错了什么?

【问题讨论】:

  • 能否提供代码示例?

标签: c# fxcop suppression


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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
    }
    

    【讨论】:

      【解决方案3】:

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

      <DebugType>full</DebugType>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 2011-12-28
        • 2013-05-08
        • 1970-01-01
        相关资源
        最近更新 更多