[英]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.