How does this class implement IDisposable if it doesn't have a Dispose method?

爱⌒轻易说出口 提交于 2019-12-06 02:44:32

问题


FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. How is that possible?


回答1:


It does have the Dispose method through inheritance, but it is an explicit implementation. 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.




回答2:


Its implemented in the base class WebResponse, see http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx




回答3:


When you implement an interface explicitly, you won't get the method in the listing. You will have to cast that object to implemented interface to get access to that method.

public class MyClass : IDisposable
{
    void IDisposable.Dispose()
    {
        throw new NotImplementedException();
    }
}

Reference : http://msdn.microsoft.com/en-us/library/ms173157.aspx




回答4:


It's implemented in the base class WebResponse

void IDisposable.Dispose()
{
try
{
    this.Close();
    this.OnDispose();
}
catch
{
}
}

alt text http://img227.imageshack.us/img227/2428/redgatesnetreflector.png




回答5:


It inherits from System.Net.WebResponse which implements these methods.



来源:https://stackoverflow.com/questions/3118861/how-does-this-class-implement-idisposable-if-it-doesnt-have-a-dispose-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!