C# 不一致的辅助功能错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1632525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:33:23  来源:igfitidea点击:

Inconsistent Accessibility error

c#visual-studio-2008

提问by tearman

I tried upgrading an ASP.Net application from Visual Studio 2005 to 2008, and I tried compiling just to verify that it would compile and I received this error.

我尝试将 ASP.Net 应用程序从 Visual Studio 2005 升级到 2008,我尝试编译只是为了验证它可以编译,但我收到了这个错误。

Error   1   Inconsistent accessibility: property type 'Web.Properties.UITitleSettings' is less accessible than property 'Web.Ctrl.BasePanel.UISettings' \projectLocation\Ctrl\BasePanel.cs 25  43  ProjectName

(I removed the class path before Web.Properties and Web.Ctrl, it normally contains it)

(我删除了 Web.Properties 和 Web.Ctrl 之前的类路径,它通常包含它)

The piece of code its referencing is

它引用的那段代码是

public Properties.UITitleSettings UISettings
    {
        get
        {
            return _uiSettings;
        }
    }

I'm not quite sure what this error is attempting to say. Does the type need to be casted (invalid implicit cast between two incompatible types?) or is it a class override issue?

我不太确定这个错误试图说什么。类型是否需要强制转换(两个不兼容类型之间的隐式转换无效?)还是类覆盖问题?

采纳答案by Paul Sasik

Look at the following definition. Notice Foois visible to anyone, and its public method GetBaris also visible to anyone who can see Foo:

看看下面的定义。NoticeFoo对任何人都可见,它的 public 方法GetBar也对任何可以看到的人可见Foo

public class Foo 
{ 
    public Bar GetBar() { return new Bar(); } 
}

Here's the definition of Bar:

这是 的定义Bar

internal class Bar {}

Notice Baris internal to the assembly, whereas Foois visible to all. Foocannot expose Barto the outside world, so the compiler throws this exception.

通知Bar内部的组件,而Foo是大家有目共睹的。 Foo不能暴露Bar给外界,所以编译器抛出这个异常。

Another example would be:

另一个例子是:

public class Foo 
{ 
    public Foo.Bar GetBar() { return new Bar(); } 
    private class Bar {} 
}

Baris a private classof Fooand can only be visible to instances of Foo. Foocannot expose this type to the outside world, so the compiler throws the same exception.

Bar是 的私有类Foo并且只能对 的实例可见FooFoo无法将此类型公开给外界,因此编译器会抛出相同的异常。



Examples of refactoring:

重构的例子:

  1. Make the hidden type public

    public class Bar {}
    public class Foo { public class Bar {} }
    
  2. Encapsulation

    public class BarEncapsulator
    {
      private Bar _bar;
      internal BarEncapsulator(Bar myBar) { _bar = myBar; }
      public string BarString { get { return _bar.MyString; } }
    }
    
  3. Hide everything

    internal class Bar {}
    internal class Foo { public class Bar {} }
    
  4. Refactor it away

    public class BarEncapsulator
    {
      private string _barString;
      public string BarString { get { return _barString; } }
    }
    
  1. 公开隐藏类型

    public class Bar {}
    public class Foo { public class Bar {} }
    
  2. 封装

    public class BarEncapsulator
    {
      private Bar _bar;
      internal BarEncapsulator(Bar myBar) { _bar = myBar; }
      public string BarString { get { return _bar.MyString; } }
    }
    
  3. 隐藏一切

    internal class Bar {}
    internal class Foo { public class Bar {} }
    
  4. 重构它

    public class BarEncapsulator
    {
      private string _barString;
      public string BarString { get { return _barString; } }
    }
    

回答by Paul Sasik

Removing the "class path" won't do anything. You're just making your code less verbose... Check the definition of Properties.UITitleSettings. It'll be private or protected etc.

删除“类路径”不会做任何事情。你只是让你的代码不那么冗长......检查 Properties.UITitleSettings 的定义。它将是私有的或受保护的等。