[英]How to dispose of a class with properties that do not implement IDisposable?
[英]How does this class implement IDisposable if it doesn't have a Dispose method?
它在基类WebResponse中实现,请参阅http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx
它通过继承确实有Dispose方法,但它是一个显式实现。 要打电话,你必须使用
((IDisposable)myObject).Dispose();
或者,当然,只需将其包装在一个using
块中,因为它会为您显式调用。
当您明确实现interface
,您将无法获得列表中的方法。 您必须将该对象强制转换为已实现的interface
才能访问该方法。
public class MyClass : IDisposable
{
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
它在基类WebResponse中实现
void IDisposable.Dispose()
{
try
{
this.Close();
this.OnDispose();
}
catch
{
}
}
替代文字http://img227.imageshack.us/img227/2428/redgatesnetreflector.png
它继承自System.Net.WebResponse ,它实现了这些方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.