Can an IDispose objects not have an available Dispose method

I am using FileHelpers in one project, and the class MultiRecordEngine

public sealed class MultiRecordEngine
    : EventEngineBase<object>, IEnumerable, IDisposable

This class implements IDisposable , BUT don't have a public Dispose method...

MultiRecordEngine eng = null;
eng.Dispose(); // <---- Results in compilation error

Inspecting this class code on GitHub I can see the method implemented explicitly here, line 913:

void IDisposable.Dispose()
{
    Close();
    GC.SuppressFinalize(this);
}

So... Why cannot I invoke the method? Is this intended, and if so, is it a good practice, and in what circumstances?


It's implemented explicitly, so you need to cast to IDisposable :

((IDisposable)eng).Dispose();

explicitly implemented members are only accessible through the interface, not the implementing class.


First, you can invoke the method by casting to IDisposable :

((IDisposable)eng).Dispose();

You can also properly use the class in a using block:

using (MultiRecordEngine eng = new MultiRecordEngine())
{
..
}
链接地址: http://www.djcxy.com/p/54518.html

上一篇: 实例方法调用Dispose

下一篇: 一个IDispose对象可以不具有可用的Dispose方法