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

编程入门 行业动态 更新时间:2024-10-27 04:30:29
本文介绍了如何在.Net 1.1中的FileStream上实现IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这似乎是个点头问题,但我正在看这个问题是因为我听说有人声称您必须在FileStream上调用Close(),即使它在using块中(并且他们的代码中Close()

我知道Close()是用来调用Dispose()的,但是我想我会更加深入这是.Net 1.1代码,我的大部分经验是使用2.0。

令我震惊的是FileStream的MSDN文档具有Dispose()和.Net 2.0上的Dispose(bool),但仅适用于.Net 1.1上的Dispose(bool)。

查看装配体-在那儿,我也看到Dispose(bool),但没有Dispose()。

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

是否存在某些编译器魔术,或者我是否在某个地方丢失了Dispose()的隐藏实现(大概称为Dispose(true)或Dispose(false)吗?)

最后(没有双关语),您是否可以确定在using块中对FileStream进行作用域限定会在.Net 1.1的作用域出口处关闭流? b

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

解决方案

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

基流类显式实现Dispose(),因此,仅当将流转换为IDisposeable时,您才可以看到Dispose()。

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

(通过Reflector获得所有这些)

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)

解决方案

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)

更多推荐

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

本文发布于:2023-11-16 13:31:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Net   FileStream   IDisposable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!