简体   繁体   English

什么时候使用block调用dispose方法

[英]when does dispose method is called in using block

I am trying to test this piece of code by putting in breakpoints. 我试图通过输入断点来测试这段代码。 I want to make sure that after the using block the dispose method is called and the resources (SqlCommand), is gracefully released. 我想确保在使用块之后调用dispose方法并且优雅地释放资源(SqlCommand)。 However nowhere in the using block I hit any dispose? 然而在使用区块中我没有任何处置?

    using (SqlCommand command = new SqlCommand(queryString, connection))
{
    command.CommandType = CommandType.Text;
    command.Parameters.Add("@OrganizationID", SqlDbType.Int);
    command.Parameters["@OrganizationID"].Value = organizationId;
    connection.Open();
    SqlDataReader sqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

    try
    {
        while (sqlDataReader.Read())
        {
            //do something 
        }
    }
    finally
    {
        sqlDataReader.Close();
    }
}

The call to Dispose of IDisposable happens after the using block has finished execution, normally or abnormally (ie through an exception). using块完成执行后,正常或异常(即通过异常)发生对Dispose of IDisposable的调用。

The only way you could catch the call in a source-level debugger is when you have the source code for your IDisposable - in your case it would be the source code of SqlCommand class. 在源代码级调试器中捕获调用的唯一方法是,当您拥有IDisposable的源代码时 - 在您的情况下,它将是SqlCommand类的源代码。

One simple way of checking the way this works is to make your own IDisposable implementation, put it into a using block, and observe its behavior. 检查其工作方式的一种简单方法是创建自己的IDisposable实现,将其放入using块,并观察其行为。 The call to Dispose should follow immediately after completion of the using block. 在完成using块之后,应立即调用Dispose

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. using语句确保即使在对象上调用方法时发生异常,也会调用Dispose。 You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; 您可以通过将对象放在try块中然后在finally块中调用Dispose来实现相同的结果; in fact, this is how the using statement is translated by the compiler. 实际上,这就是编译器如何翻译using语句。 The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object): 前面的代码示例在编译时扩展为以下代码(注意额外的花括号以创建对象的有限范围):

Key part is the "achieve the same result by putting the object inside a try block and calling finally". 关键部分是“通过将对象放在try块中并最终调用来实现相同的结果”。

SqlCommand command = new SqlCommand(queryString, connection);
try {

      // your code here
} finally {

      command.Dispose();
}

From MSDN 来自MSDN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用nSubstitute时未调用抽象的Dispose方法 - Abstracted Dispose Method not called when using nSubstitute 调用dispose时用户控件不进行dispose - User control does not dispose when dispose is called 什么时候处理方法不被调用? - When would dispose method not get called? 如果我在方法中的using块内返回一个值,那么在返回之前是否使用了dispose对象? - If I return a value inside a using block in a method, does the using dispose of the object before the return? 在C#中对同一作用域使用多个using语句时,是否保证调用Dispose()方法的顺序? - Is there a guarantee on the order in which the Dispose() method is called when using multiple using statements for the same scope in C#? 是否可以通过反射检查类型是否在使用块中(调用了Dispose) - Is it possible to check via reflection that type is in using block (Dispose is called) 是否存在“使用”块不会调用 Dispose 的情况? - Is there a situation in which Dispose won't be called for a 'using' block? IDisposable.Dispose 在使用块异常后永远不会被调用 - IDisposable.Dispose is never called after exception in using block 如果我在方法中使用 SqlCommand 作为参数,它会阻止 SqlConnection 处置吗? - If I use SqlCommand in a method as a parameter, does it block a SqlConnection dispose? 在using语句中抛出异常时,是否仍会调用Dispose? - Does Dispose still get called when exception is thrown inside of a using statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM