为什么我需要调用Dispose ASP.NET控件? [英] Why would I need to call dispose on ASP.NET Controls?

查看:102
本文介绍了为什么我需要调用Dispose ASP.NET控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些VS ASP.NET开发,并刚刚发现一个有趣的小code的建议(我认为他们来自$ C $美眉但我可能是错的)。

I'm doing some ASP.NET development in VS and have just found an interesting little code suggestion (I think they come from coderush but I could be wrong).

每当我创建控件它告诉我,我应该使用使用声明他们。我有点困惑,到底是怎么回事这里虽然。与使用我的code看起来是这样的:

Whenever I'm creating controls it tells me that I should be using a "using" statement for them. I'm a bit confused as to what is going on here though. with the using my code looks something like:

using (HtmlTableRow tableRow = new HtmlTableRow())
{
    tableRow.Attributes.Add("class", isOddRow ? "OddRow" : "EvenRow");
    listingTable.Rows.Add(tableRow);
    addCell(tableRow, row, "issueId");
    addCell(tableRow, row, "Title");
    addCell(tableRow, row, "Type");
    addCell(tableRow, row, "Summary");
}

所以我期待,在using语句的结束,它会调用上的TableRow处理。然而,在MSDN库中的文档说:

So I am expecting that at the end of the using statement it will call dispose on the tableRow. However, the docs in MSDN library says:

Dispose方法离开控制
  处于不可用状态。打完电话后
  这种方法,你必须释放所有
  在控制引用所以
  内存有人占用可
  通过垃圾回收。

The Dispose method leaves the Control in an unusable state. After calling this method, you must release all references to the control so the memory it was occupying can be reclaimed by garbage collection.

所以,我希望,我现在已经在我的控制结构不能使用的对象,所以它会破坏或无法渲染什么的。然而,一切似乎都工作得很好。

So I would expect that I now have an unusable object in my control structure so it would break or not render or something. However, everything seems to work just fine.

所以,我想知道就是为什么都是一次性的控制?难道仅仅是因为他们中的一些会,使他们可支配的所有手段,一个呼叫在顶层然后可以向下传递给所有子处理递归控制?

So what I'm wondering is why are all controls disposable? Is it just because some of them will be and making them all disposable means that one call to dispose at the top level can then be passed down to all child controls recursively?

我想如果不是我理解的事实,文档明确地说,控制处置,使之无法使用......都是刚刚文档错了?

I think I'd understand if not for the fact that the docs explicitly say that disposing of a control makes it unusable... Are the docs just wrong?

推荐答案

您不应该真正做到这一点。你需要做的是确保listingTable是在控件收集这样页处置时,将进行处理。该listingTable对象则负责妥善弃置所有的孩子。

You shouldn't actually do this. What you need to do is make sure that listingTable is in the Controls collection so that it will be disposed when the Page is disposed. The listingTable object is then responsible for properly disposing all of its children.

这就是为什么所有的控制对象实现IDisposable接口。这样,每个家长控制可以调用的Dispose 对其所有的孩子,而不必首先测试/施放每一个。每个控制是决定其是否实际上有任何需要清理时,其的Dispose 方法被称为个人责任。

Which is why all Control objects implement the IDisposable interface. That way each parent control can call Dispose on all of its children without first having to test/cast each one. Each control is individually responsible for determining whether it actually has anything that needs to be cleaned up when its Dispose method is called.

该文档都没有错。实现了IDisposable的和任何正确编写的对象具有在处置过程实际上是清理状态数据的应该抛出的ObjectDisposedException如果任何公共/保护/内部属性或方法就已经后被访问处置。 (的Dispose 被称为后假设无效状态。)某些类型会忽略这个规则,如果他们实际上没有任何东西进行清理,而不必担心无效状态。

The docs are not wrong. Any properly-written object that implements IDisposable and has state data that is actually cleaned up during the dispose process should throw an ObjectDisposedException if any of its public/protected/internal properties or methods are accessed after it has been disposed. (Assume invalid state after Dispose has been called.) Some types will ignore this rule if they don't actually have anything to clean up, and don't have to worry about invalid state.

原因你得到一个建议,把它包在使用块,因为分析器没有意识到 listingTable 将出售其集合,这将处理每一个已被添加到它的行对象。此外,如果有异常 HtmlTableRow的TableRow =之间抛出新HtmlTableRow() listingTable.Rows.Add(tablerow的) ,该HtmlTableRow对象将被'孤儿',而不是在其他任何对象的的IDisposable 层次。 code分析要你使用try / finally块来处理立即的 HtmlTableRow 如果这种情况发生。

The reason you're getting a suggestion to wrap it in a using block is because the analyzer doesn't realize that listingTable will dispose its Rows collection, which will dispose each of the row objects that have been added to it. Also, if an exception is thrown between HtmlTableRow tableRow = new HtmlTableRow() and listingTable.Rows.Add(tableRow), the HtmlTableRow object will be 'orphaned' and not be in any other object's IDisposable hierarchy. Code Analysis wants you to use a try/finally block to immediately dispose of the HtmlTableRow if this happens.

这篇关于为什么我需要调用Dispose ASP.NET控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆