公共类无法访问由于其保护级别 [英] Public class is inaccessible due to its protection level

查看:202
本文介绍了公共类无法访问由于其保护级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

namespace Bla.Bla 
{
    public abstract class ClassA 
    {
        public virtual void Setup(string thing) 
        {
        }

        public abstract bool IsThingValid();

        public abstract void ReadThings();

        public virtual void MatchThings() { }

        public virtual void SaveThings() { }

        public void Run(string thing) 
        {
            Setup(thing);

            if (!IsThingValid()) 
            {

            }

            ReadThings();
            MatchThings();
            SaveThings();
        }
    }
}

namespace Bla.Bla 
{
    public class ClassB : ClassA 
    {
        ClassB() { } 

        public override void IsThingValid() 
        {
            throw new NotImplementedException();
        }

        public override void ReadThings() 
        {
            throw new NotImplementedException();
        }
    }
}

现在我尝试做以下内容:

Now I try to do the following:

public class ClassC 
{
    public void Main() 
    {
        var thing = new ClassB();
        ClassB.Run("thing");
    }
}



它返回以下错误:ClassB的是人迹罕至因它的保护级别。

Which returns the following error: ClassB is inaccessible due to its protection level.

但他们都是公开的。

推荐答案

此错误是 ClassB的的保护级别,因此小号的构造的,而不是 ClassB的本身。由于构造函数的名称相同的类的名称 * ,该错误可能被错误地解释。既然你没有指定你的构造的保护级别,它被认为是内部默认情况下。声明构造公共将解决此问题:

This error is a result of the protection level of ClassB's constructor, not ClassB itself. Since the name of the constructor is the same as the name of the class* , the error may be interpreted incorrectly. Since you did not specify the protection level of your constructor, it is assumed to be internal by default. Declaring the constructor public will fix this problem:

public ClassB() { } 

* 你也可以说,构造函数没有名字,只有一种;这不会改变问题的本质。

* One could also say that constructors have no name, only a type; this does not change the essence of the problem.

这篇关于公共类无法访问由于其保护级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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