有没有一种方法来检查对象是否已被处置不同
try
{
myObj.CallRandomMethod();
} catch (ObjectDisposedException e)
{
// now I know object has been disposed
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我正在使用TcpClient
具有Close()
处理对象的方法的类,这可能发生在我无法控制的代码段中.在这种情况下,我想有更好的解决方案然后捕获异常.
Han*_*ant 32
一个好方法是从TcpClient派生并覆盖Disposing(bool)方法:
class MyClient : TcpClient {
public bool IsDead { get; set; }
protected override void Dispose(bool disposing) {
IsDead = true;
base.Dispose(disposing);
}
}
Run Code Online (Sandbox Code Playgroud)
如果其他代码创建了实例,那将无效.然后你将不得不做一些绝望的事情,比如使用Reflection获取私有m_CleanedUp成员的值.或者抓住例外.
坦率地说,没有一个可能会达到一个很好的结果.你真的没有想写的TCP端口.但你不会,你无法控制的错误代码现在可以控制你的代码了.你增加了bug的影响.与该代码的所有者交谈并解决问题是迄今为止最好的解决方案.
编辑:一个反映的例子:
using System.Reflection;
public static bool SocketIsDisposed(Socket s)
{
BindingFlags bfIsDisposed = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty;
// Retrieve a FieldInfo instance corresponding to the field
PropertyInfo field = s.GetType().GetProperty("CleanedUp", bfIsDisposed);
// Retrieve the value of the field, and cast as necessary
return (bool)field.GetValue(s, null);
}
Run Code Online (Sandbox Code Playgroud)
Rya*_*ner 16
如果您不确定对象是否已被处置,则应调用Dispose
方法本身而不是方法Close
.虽然框架不能保证Dispose方法必须在没有异常的情况下运行,即使该对象先前已被处理过,但这是一种常见的模式,而且我的知识是在框架中的所有一次性对象上实现的.
对于典型的模式Dispose
,按照微软:
public void Dispose()
{
Dispose(true);
// Use SupressFinalize in case a subclass
// of this type implements a finalizer.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// If you need thread safety, use a lock around these
// operations, as well as in your methods that use the resource.
if (!_disposed)
{
if (disposing) {
if (_resource != null)
_resource.Dispose();
Console.WriteLine("Object disposed.");
}
// Indicate that the instance has been disposed.
_resource = null;
_disposed = true;
}
}
Run Code Online (Sandbox Code Playgroud)
注意检查_disposed
.如果您要调用Dispose
实现此模式的方法,则可以根据需要多次调用Dispose,而不会遇到异常.
归档时间: |
|
查看次数: |
111556 次 |
最近记录: |