【发布时间】:2011-06-01 17:27:59
【问题描述】:
interface IMyInterace
{
void Open();
object Read();
void Close();
}
class MyImplementation : IMyInterface
{
public void Open() { /* instantiates disposible class */ }
//...
public void Close() { /* calls .Dispose(); */ }
}
是否有处理这种情况的好方法,以确保调用类中的可处置实例? (除了在文档中,没有信号通知调用者他们必须调用“关闭”。)IMyInterface 的实现不一定封装 IDisposible 实例,并且在应用程序的整个生命周期中反复关闭和重新打开。
我正在考虑这样做:
- 在 MyImplementation 中实现 IDisposible。
- 将 Dispose() 设置为调用 Close()。
- 添加对 Close() 或 Dispose() 的调用到 开始开放,以确保以前 通话已结束。
IMyInterface 的用户不知道他们使用的是什么实现,所以我不确定使 MyImplementation disposible 有多少价值,而且并非所有实现都会封装 IDisposibles。
【问题讨论】:
标签: c# idisposable resource-management