IDispose对象可以没有可用的Dispose方法吗 [英] Can an IDispose objects not have an available Dispose method

查看:91
本文介绍了IDispose对象可以没有可用的Dispose方法吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用 FileHelpers 和类 MultiRecordEngine

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

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

此类实现了 IDisposable ,但是没有公共的Dispose方法...

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

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

检查 GitHub上的此类代码我可以参见此处明确实现的方法,第913行:

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

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

所以... 为什么我不能调用该方法?这是有意的吗?如果是这样,这是一种好习惯吗?在什么情况下?

推荐答案

它是明确实现的,因此您需要转换为 IDisposable :

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.

这篇关于IDispose对象可以没有可用的Dispose方法吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆