一个人如何知道如果一个IDisposable的对象引用配置? [英] How does one tell if an IDisposable object reference is disposed?

查看:134
本文介绍了一个人如何知道如果一个IDisposable的对象引用配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,或其他一些轻量的方式,来检查,如果引用的是一个释放的对象?

Is there a method, or some other light-weight way, to check if a reference is to a disposed object?

P.S。 - 这只是一个好奇(睡不好觉,而不是在生产代码)。是的,我知道我能赶上的ObjectDisposedException 在试图访问对象的成员。

P.S. - This is just a curiousity (sleep well, not in production code). Yes, I know I can catch the ObjectDisposedException upon trying to access a member of the object.

推荐答案

这要看,有的IDisposable 的对象,允许调用的Dispose 方法尽可能你想,有抛出的ObjectDisposedException 的IDisposable 的对象。在这些对象必须跟踪状态,这种情况下(通常是用私人布尔字段 isDisposed 实施)。

It depends, there are IDisposable objects that allow to call the Dispose method as much as you want, and there are IDisposable objects that throw ObjectDisposedException. In such a case these objects must track the state (usually implemented with a private boolean field isDisposed).

这篇关于一个人如何知道如果一个IDisposable的对象引用配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆