抛出 ObjectDisposeException 时应该传递什么作为 objectName?
在实现 IDisposable 时,我知道在对象被释放后不应调用的每个方法都应抛出 ObjectDispositedException
。但是应该传递给异常构造函数的名称对象的标准是什么?
When implementing IDisposable, I undertand that every method that shouldn't be called after the object's been disposed should throw the ObjectDisposedException
. But what is the standard for the name object that should be passed to the exception's constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信推荐的做法是抛出以下内容:
或者包括检查,这两行代码位于需要它的每个方法的顶部(显然不是
Dispose
方法本身):甚至可能会有所帮助将其重构为一个小方法以提高可用性。
I believe the recommended practice is to throw the following:
Or including the check, these two lines of code at the top of each method that needs it (obviously not the
Dispose
method itself):Might even be helpful to refactor this into a tiny method for usability.
甚至 .NET Framework 本身在这里也不是很一致。
David M. Kean(Microsoft FxCop 团队的前开发人员)向 ObjectDisposeException 的 MSDN 文档:
Even the .NET Framework itself isn't very consistent here.
David M. Kean (former developer on the FxCop team at Microsoft) added a comment to the MSDN documentation for the ObjectDisposedException:
我不相信有一个标准,我会返回对象的类型以及唯一标识字段(某种“主键”)的字符串内容。
I don't believe there's a standard for that, I would return the type of the object along with the string content of a unique identifying field (a 'Primary Key' of sorts).