-3

I've got a class that implements an interface:

public class SQLiteHHSDBUtils : IHHSDBUtils
{

    void IHHSDBUtils.SetupDB()
    {
            . . .
            if (!TableExists("AppSettings"))

    . . .

    bool IHHSDBUtils.TableExists(string tableName)
    {
    . . .

It can't find its own brother sitting right below it (the if (!TableExists()):

The name 'TableExists' does not exist in the current context

How can it / why does it not see it?

4

3 回答 3

6

您有一个显式的接口实现。除非您将当前实例转换为接口类型,否则您不能直接访问您的接口方法:

if (!((IHHSDBUtils)this).TableExists("AppSettings"))

13.4.1 显式接口成员实现

在方法调用、属性访问或索引器访问中,无法通过其完全限定名称访问显式接口成员实现。显式接口成员实现只能通过接口实例访问,并且在这种情况下仅通过其成员名称引用。

于 2014-12-01T22:20:27.100 回答
3

当你显式实现一个接口时,你需要从一个类型正好是接口(不是实现类型)的变量访问接口成员。

if (!TableExists("AppSettings"))正在TableExists通过this对象调用,其类型为SQLiteHHSDBUtils,而不是IHHSDBUtils

尝试:

if (!((IHHSDBUtils)this).TableExists("AppSettings"))

或者,不要显式实现接口:

public class SQLiteHHSDBUtils : IHHSDBUtils
{
    // .. 
    bool TableExists(string tableName)
    {
        // ..
于 2014-12-01T22:20:50.423 回答
2

TableExists是一个显式的实现。如果要访问它,则必须强制this转换为IHHSDBUtils

void IHHSDBUtils.SetupDB()
{
    . . . 
    if (!((IHHSDBUtils)this).TableExists("AppSettings"))
于 2014-12-01T22:20:29.337 回答