[英]How does this class implement IDisposable if it doesn't have a Dispose method?
FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. FtpWebResponse实现了IDisposable,但它没有Dispose方法。 How is that possible?
怎么可能?
它在基类WebResponse中实现,请参阅http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx
It does have the Dispose method through inheritance, but it is an explicit implementation. 它通过继承确实有Dispose方法,但它是一个显式实现。 To call it, you would have to use
要打电话,你必须使用
((IDisposable)myObject).Dispose();
Or, of course, just wrap it in a using
block, as it does the explicit call for you. 或者,当然,只需将其包装在一个
using
块中,因为它会为您显式调用。
When you implement an interface
explicitly, you won't get the method in the listing. 当您明确实现
interface
,您将无法获得列表中的方法。 You will have to cast that object to implemented interface
to get access to that method. 您必须将该对象强制转换为已实现的
interface
才能访问该方法。
public class MyClass : IDisposable
{
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
Reference : http://msdn.microsoft.com/en-us/library/ms173157.aspx 参考: http : //msdn.microsoft.com/en-us/library/ms173157.aspx
It's implemented in the base class WebResponse 它在基类WebResponse中实现
void IDisposable.Dispose()
{
try
{
this.Close();
this.OnDispose();
}
catch
{
}
}
alt text http://img227.imageshack.us/img227/2428/redgatesnetreflector.png 替代文字http://img227.imageshack.us/img227/2428/redgatesnetreflector.png
它继承自System.Net.WebResponse ,它实现了这些方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.