通过注释看似无关的代码可以删除CA2000警告

时间:2012-06-15 19:02:12

标签: c# .net code-analysis idisposable

问题是VS2010代码分析正在为特定功能返回两个CA2000警告。我没有成功地用较小的代码块重现警告,所以我已经完整地发布了原始函数。

public int SaveTransaction( Transaction tx, UserAccount account ) {

        if ( tx == null ) {
            throw new ArgumentNullException( "tx" );
        }

        if ( account == null ) {
            throw new ArgumentNullException( "account" );
        }

        bool isRefund = tx.TransactionType == LevelUpTransaction.TransactionTypes.Refund;

        int pnRef = 0;

        using ( SqlConnection conn = new SqlConnection( DatabaseConfiguration.ConnectionString ) ) {

            using ( SqlCommand cmd = new SqlCommand( "dbo.SaveTransaction", conn ) ) {

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add( "@InvoiceId", SqlDbType.VarChar, 100 ).Value = tx.InvoiceNumber;
                cmd.Parameters.Add( "@TxStartDate", SqlDbType.DateTime ).Value = tx.TransactionBeginDate;
                cmd.Parameters.Add( "@AuthDate", SqlDbType.DateTime ).Value = tx.AuthenticationDate;
                cmd.Parameters.Add( "@MerchantKey", SqlDbType.Int ).Value = account.MerchantKey;
                cmd.Parameters.Add( "@UserName", SqlDbType.Char, 25 ).Value = account.UserName;
                cmd.Parameters.Add( "@RegisterNumber", SqlDbType.Char, 10 ).Value = tx.RegisterNumber;
                cmd.Parameters.Add( "@ResellerKey", SqlDbType.Int ).Value = account.ResellerKey;
                cmd.Parameters.Add( "@TxEndDate", SqlDbType.DateTime ).Value = tx.TransactionEndDate;
                cmd.Parameters.Add( "@IpAddress", SqlDbType.VarChar, 15 ).Value = account.IPAddress;
                cmd.Parameters.Add( "@CustomerId", SqlDbType.VarChar, 50 ).Value = tx.CustomerId;
                cmd.Parameters.Add( "@TransactionId", SqlDbType.VarChar, 50 ).Value = tx.TransactionId;
                cmd.Parameters.Add( "@ProcStartDate", SqlDbType.DateTime ).Value = tx.ProcessorBeginDate;
                cmd.Parameters.Add( "@ProcEndDate", SqlDbType.DateTime ).Value = tx.ProcessorEndDate;
                cmd.Parameters.Add( "@AuthAmount", SqlDbType.Money ).Value = StringParser.ParseDecimal( tx.OriginalAmount );
                cmd.Parameters.Add( "@ResultCode", SqlDbType.VarChar, 50 ).Value = tx.ResultCode;
                cmd.Parameters.Add( "@ResultMessage", SqlDbType.VarChar, 150 ).Value = tx.ResultMessage;
                cmd.Parameters.Add( "@PONumber", SqlDbType.VarChar, 100 ).Value = tx.PurchaseOrderNumber;
                cmd.Parameters.Add( "@TaxAmount", SqlDbType.Money ).Value = StringParser.ParseDecimal( tx.TaxAmount );
                cmd.Parameters.Add( "@Refund", SqlDbType.Bit ).Value = isRefund;

                if ( tx.Order != null ) {
                    cmd.Parameters.Add( "@HostDate", SqlDbType.VarChar, 50 ).Value = tx.Order.HostTime.ToString();
                    cmd.Parameters.Add( "@ApprovalCode", SqlDbType.VarChar, 50 ).Value = tx.Order.TransactionId.ToString( CultureInfo.InvariantCulture );
                    cmd.Parameters.Add( "@NameOnCard", SqlDbType.VarChar, 200 ).Value = tx.Order.UserFirstName + " " + tx.Order.UserLastNameInitial;
                    cmd.Parameters.Add( "@TipAmount", SqlDbType.Money ).Value = StringParser.ParseDecimal( tx.Order.Tip.FormattedAmount );
                    cmd.Parameters.Add( "@TotalAmount", SqlDbType.Money ).Value = StringParser.ParseDecimal( tx.Order.TotalAmount.FormattedAmount );
                    cmd.Parameters.Add( "@DiscountAmount", SqlDbType.Money ).Value = StringParser.ParseDecimal( tx.Order.CreditAmount.FormattedAmount );
                }

                else  {
                    cmd.Parameters.Add( "@NameOnCard", SqlDbType.VarChar, 200 ).Value = DBNull.Value;
                    cmd.Parameters.Add( "@HostDate", SqlDbType.VarChar, 50 ).Value = DBNull.Value;
                    cmd.Parameters.Add( "@ApprovalCode", SqlDbType.VarChar, 50 ).Value = DBNull.Value;
                    cmd.Parameters.Add( "@TipAmount", SqlDbType.Money ).Value = 0;
                    cmd.Parameters.Add( "@TotalAmount", SqlDbType.Money ).Value = 0;
                    cmd.Parameters.Add( "@DiscountAmount", SqlDbType.Money ).Value = 0;
                }

                if ( isRefund ) {
                    cmd.Parameters.Add( "@OriginalPnRef", SqlDbType.Int ).Value = tx.OriginalToken;
                }

                conn.Open();

                using ( SqlDataReader dr = cmd.ExecuteReader() ) {

                    while ( dr.Read() ) {
                        pnRef = SqlNull.Integer( dr["TRX_HD_Key"] );
                    }
                }
            }

        }

        return pnRef;
    }

两个CA2000警告属于包含SqlConnectionSqlCommand的using语句。

我在代码本身找不到任何问题,但我发现随机注释掉行会使错误消失。例如,注释掉else块中设置为0的三个货币字段将删除警告。相反,在末尾用DBNull.Value注释掉三行也将删除错误。我无法理解结果。

1 个答案:

答案 0 :(得分:4)

这已在Microsoft Connect报告为分析器中可能存在的错误。显然,如果using语句包含大量代码,则会错误地报告此消息。

可以通过以下方式进行抑制:

[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Reliability", "CA2000:Dispose objects before losing scope" )]