在 WP7 上释放 Application.GetResourceStream 返回的底层流 [英] Releasing underlying Stream returned by Application.GetResourceStream on WP7
问题描述
当使用 Application.GetResourceStream
访问资源时,它返回一个 StreamResourceInfo
类的实例,这个类没有实现 IDisposable.
When accessing a resource using Application.GetResourceStream
it returns an instance of the class StreamResourceInfo
, this class does not implement IDisposable.
当我在 WP7 设备上完成处理流时,我是否应该关闭\处置由 StreamResourceInfo
公开的底层流 - 我不想知道 Using
或处置模式.
Should I close\dispose the underlying stream exposed by StreamResourceInfo
when I've finished processing the stream on a WP7 device - I don't want to know about Using
or the Dispose pattern.
一个例子是使用该方法访问包含在 ZAP 包中的 ZIP 文件,一旦我解压了该文件,我就不再需要 Stream
实例.
An example would be using the method to access a ZIP file which is contained with the ZAP package and once I have unzipped the file I don't need the Stream
instance anymore.
推荐答案
提供的代码示例 这里 没有使用 using
、Close()
或 Dispose()
.
The code example provided here does not make use of using
, Close()
or Dispose()
.
因为 StreamResourceInfo
的文档没有提到 Close()
或 Dispose()
(和 StreamResourceInfo
实际上并不从 Stream
或任何其他可能实现这些方法的东西继承),我认为让对象超出范围并自然地进行垃圾收集是安全的.
Since the documentation for StreamResourceInfo
doesn't mention Close()
or Dispose()
(and StreamResourceInfo
does not actually inherit from Stream
or anything else that might implement these methods), I think it's safe to just let the object go out of scope and garbage collect naturally.
我怀疑 StreamResourceInfo
类有一个终结器,它在垃圾回收期间在底层 Stream
对象上调用 Dispose()
,但我没有我目前没有可用的 Reflector 副本来验证这一点.IDisposable
模式在这方面非常强大.
I suspect that the StreamResourceInfo
class has a finalizer that calls Dispose()
on the underlying Stream
object during garbage collection, but I don't have a copy of Reflector available to me at the moment to verify that. The IDisposable
pattern is quite robust in that regard.
但是,如果您仍然对这种不确定性感到不舒服,您可以随时在 Stream
对象上调用 Close()
.
However, if you still feel uncomfortable with that level of uncertainty, you can always call Close()
on the Stream
object.
这篇关于在 WP7 上释放 Application.GetResourceStream 返回的底层流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!