我有以下课程:
public class MyClass : IDisposable
{
private WebServiceHost m_WebServiceHost;
// Members
public void Dispose()
{
m_WebServiceHost // how do I dispose this object?
}
}
WebServiceHost
实施IDisposable
,但它没有Dispose
方法。
如何实施Dispose()
?
答案 0 :(得分:3)
鉴于它使用explicit interface implementation,我不清楚他们是否想要你,但你可以:
public class MyClass : IDisposable
{
private WebServiceHost m_WebServiceHost;
// Members
public void Dispose()
{
((IDisposable)m_WebServiceHost).Dispose();
}
}
我会猜测他们更喜欢你只是打电话给Close()
,但我还是不能从文档中支持。
答案 1 :(得分:2)
这样做:
public class MyClass : IDisposable
{
private WebServiceHost m_WebServiceHost;
// Often you have to override Dispose method
protected virtual void Dispose(Boolean disposing) {
if (disposing) {
// It looks that WebServiceHost implements IDisposable explicitly
IDisposable disp = m_WebServiceHost as IDisposable;
if (!Object.RefrenceEquals(null, disp))
disp.Dispose();
// May be useful when debugging
disp = null;
}
}
// Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}