How does this class implement IDisposable if it do

2019-04-09 09:53发布

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

5条回答
啃猪蹄的小仙女
2楼-- · 2019-04-09 10:16

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

查看更多
Root(大扎)
3楼-- · 2019-04-09 10:21

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

查看更多
在下西门庆
4楼-- · 2019-04-09 10:22
兄弟一词,经得起流年.
5楼-- · 2019-04-09 10:32

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.

查看更多
ら.Afraid
6楼-- · 2019-04-09 10:40

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

查看更多
登录 后发表回答