简体   繁体   English

如果没有Dispose方法,这个类如何实现IDisposable?

[英]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? 怎么可能?

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.

相关问题 如何处置具有无法实现IDisposable的属性的类? - How to dispose of a class with properties that do not implement IDisposable? 如果我有一个Dispose方法,我必须实现IDisposable吗? - If I have a Dispose method, must I implement IDisposable? WebResponse在实现IDisposable时如何不公开显示“Dispose”? - How does WebResponse not have “Dispose” publically visible when it implements IDisposable? NUnit是否处置实现IDisposable的对象? - Does NUnit dispose of objects that implement IDisposable? 在实现IDisposable的类上正确使用Dispose方法 - Correct usage of Dispose method on class that implements IDisposable 为什么我必须从IDisposable实现Dispose,为什么不只是一个简单的方法来释放...? - Why Do I have to implement Dispose from IDisposable, Why not just a simple method to release…? 处置未实现IDisposable的第三方类使用的资源 - Dispose of resources used by 3rd party class which does not implement IDisposable 当我的控制器的字段实现IDisposable时,该如何处置? - How do I dispose of my Controllers when their fields implement IDisposable? 当dispose()方法可以写在类中时,为什么要使用IDisposable - why use IDisposable when a dispose() method could be written in a class IDisposable.Dispose()作为方法名称是什么意思? - What does IDisposable.Dispose() as a method name mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM