我有以下类:
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.
更多推荐
公共类无法访问由于其保护级别
发布评论