.Net 1.1 中 IDisposable 如何在 FileStream 上实现

发布于 2024-07-21 22:27:39 字数 806 浏览 3 评论 0原文

这似乎是一个棘手的问题,但我之所以关注这个问题,是因为我听到有人声称必须在 FileStream 上调用 Close(),即使它位于 using 块中(并且他们有调用 Close() 的代码)就在块的末尾)。

我知道 Close() 旨在调用 Dispose(),但我想我应该更深入地研究,因为这是 .Net 1.1 代码,而我的大部分经验都是在 2.0 上使用的。

令我印象深刻的一件事是,FileStream 的 MSDN 文档针对 .Net 2.0 提供了 Dispose() 和 Dispose(bool),但仅针对 .Net 1.1 提供了 Dispose(bool)。

我认为这可能是一个疏忽,所以我使用 Reflector 来查看程序集 - 在那里我也看到了 Dispose(bool),但没有看到 Dispose()。

它是否正确? 如果是这样,这里有什么故事? FileStream 在 using 块中工作 - 我认为这意味着它必须实现 IDisposable,据我所知,IDisposable 仅声明了 Dispose()。

是否存在一些编译器魔法,或者我是否在某个地方缺少 Dispose() 的隐藏实现(大概会调用 Dispose(true) 或 Dispose(false) ?)

最后(没有双关语),您能否确认范围using 块中的 FileStream 将在 .Net 1.1 中的作用域出口处关闭流?

[编辑]

只是澄清一下,这是 C# 代码。 我知道 VB.Net 直到 .Net 2.0 才得到 using 语句,但我的理解是 C# 在 1.1 中有它(我的 1.1 代码在这里有它并且可以编译)

This might seem like a noddy question, but I was looking at this because I heard someone claiming that you must call Close() on a FileStream, even if it is in a using block (and they have code where Close() is being called right at the end of the block).

I know that Close() is meant to call Dispose(), but I thought I'd look deeper since this is .Net 1.1 code, and the bulk of my experience has been with 2.0 on.

One thing that struck me was that the MSDN documentation for FileStream has Dispose() and Dispose(bool) for .Net 2.0 on, but only Dispose(bool) for .Net 1.1.

I thought that might be an oversight, so I used Reflector to look into an assembly - and there too, I see Dispose(bool), but no Dispose().

Is this correct? If so, what's the story here? FileStream works in a using block - which I thought meant it must implement IDisposable which, to my knowledge only declares Dispose().

Is there some compiler magic going on, or am I missing a hidden implementation of Dispose() somewhere (which, presumably, calls Dispose(true) or Dispose(false) ? )

Finally (no pun intended), can you confirm that scoping a FileStream in using block will close stream at scope exit in .Net 1.1?

[edit]

Just to clarify, this is C# code. I understand that VB.Net didn't get the using statement until .Net 2.0, but my understanding is that C# had it in 1.1 (and my 1.1 code here has it in and it compiles)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2024-07-28 22:27:39

它的实现有点有趣,但它就在那里:
FileStream的基类:System.IO.Stream实现了IDisposable(FileStream只是继承了它)。

基流类显式实现 Dispose(),因此如果将流强制转换为 IDisposeable(这就是 using{} 的作用),您只会看到 Dispose()。

Stream.Dispose() 调用 Stream.Close()。

(通过 Reflector 获得所有这些)

It's implemented a little funny, but it's there:
The base class for FileStream: System.IO.Stream implements IDisposable (FileStream just inherits it).

The base stream class implements Dispose() explicitly, so you will only see Dispose() if you cast the stream as IDisposeable (which is what using{} does).

Stream.Dispose() calls Stream.Close().

(got all this via Reflector)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文