【问题标题】:Member variable inaccessible due to its protection level成员变量由于其保护级别而无法访问
【发布时间】:2020-01-23 01:25:59
【问题描述】:

我试图在我的实现中访问一个变量,它指出“由于其保护级别而无法访问”

namespace mytest
{
    public class IDatabaseSettings
    {
        string connection { get; set; }
    }
}

namespace mytest
{
    public class DatabaseSettings : IDatabaseSettings
    {
        public string connection{ get; set; }
    }
}

IDatabaseSettings settings = new DatabaseSettings();
settings.connection <--- is inaccessible due to its protection level

关于为什么会发生这种情况的任何建议?

【问题讨论】:

  • 您的意思是将IDatabaseSettings 声明为接口而不是类?

标签: c#


【解决方案1】:

虽然您有一个公共类,但您创建的属性是私有的。当您没有在 class 上指定访问修饰符时,默认值是私有的。接口不允许访问修饰符,因为一切都假定为公共的(否则接口将无用)。

在你的 DatabaseSettings 类上使用它(界面很好):

public string connection { get; set; }

此外,您的接口被定义为一个类。改为将其定义为接口:

public interface IDatabaseSettings
{
    ...
}

【讨论】:

  • 对不起,我已经公开了,但我仍然收到这条消息
  • 啊,你的接口被定义为一个类。将public class IDatabaseSettings 更改为public interface IDatabaseSettings
  • 是的。你搞定了。谢谢你。标记为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2011-09-01
  • 1970-01-01
  • 2011-04-02
  • 2011-02-07
相关资源
最近更新 更多