代码之家  ›  专栏  ›  技术社区  ›  icebox19

可访问性不一致:返回类型的可访问性低于方法C#

  •  12
  • icebox19  · 技术社区  · 10 年前

    好吧,这真的很糟糕。我有一个私人会员,我想把它用到Form2。我已经创建了一个公共静态方法,这样我就可以将该成员放入Form2。

    这是我的代码:

    private static AppController appController;
    private BreadRepository breadRep;
    private CakeRepository cakeRep;
    private SandwichRepository sandwichRep;
    
    public Form1()
    {
        InitializeComponent();
    
        breadRep = new BreadRepository();
        cakeRep = new CakeRepository();
        sandwichRep = new SandwichRepository();
        appController = new AppController(breadRep , sandwichRep, cakeRep);
    }
    public static AppController getController()
    {
        return appController;
    }
    

    我已经尝试将Form1中的appController公开,但我收到了更多错误。现在我明白了:

    可访问性不一致:返回类型“example_map.controller.AppController”的可访问性低于方法“example_map.Form1.getController()” 有什么想法吗?

    更新:

    这是我的AppController类:

    class AppController
    {
        private BreadRepository breadRep;
        private SandwichRepository sandwichRep;
        private CakeRepository cakeRep;
        public AppController(BreadRepository breadRep, SandwichRepository sandwichRep, CakeRepository cakeRep)
        {
            this.breadRep = breadRep;
            this.sandwichRep = sandwichRep;
            this.cakeRep = cakeRep;
        }
    
        public void writeToFile(String file)
        {
            StreamWriter wr = new StreamWriter(file);
            String writeMe = "";
            foreach(Bread e in breadRep.getAll())
            {
                writeMe = writeMe + e.getAll() + "\n";
            }
            foreach (Sandwich e in sandwichRep.getAll())
            {
                writeMe = writeMe + e.getAll() + "\n";
            }
            foreach (Cake e in cakeRep.getAll())
            {
                writeMe = writeMe + e.getAll() + "\n";
            }
    
            wr.Write(writeMe);
            wr.Close();
        }
    }
    

    我已将AppController更改为public,但再次出现更多错误。同样的错误,但对于breadRep、cakeRep和sandwichRep。

    2 回复  |  直到 10 年前
        1
  •  40
  •   AlexD    10 年前

    问题是,正如@Selman22所解释的,您的方法是 public ,而其返回值为 internal 。(类为 内部的 默认情况下。)

    如果两者都是 平民的 内部的 ,一切都应该正常。

    正在制作类 平民的 由于依赖于其他类,这似乎很困难。此外,这可能不是最好的,因为默认情况下,最好是让东西不那么容易访问。

    制定方法 内部的 从另一端解决了同样的问题。

    无论如何,@Selman22是第一名:)。我刚刚加了两美分,所以你也许应该接受他的回答:)。

        2
  •  6
  •   Charleh    10 年前

    可访问性由类型或成员的访问级别决定。需要注意的是,类型/类型成员的默认访问级别不同

    类型的默认访问级别为 内部的

    成员的默认访问级别为 私有的

    还需要注意的是 私有的 不适用于类型(如果类型是私有的,您如何构造它?它只能构造自己),除非该类型嵌套在另一个类型中

    知道了这一点,很容易理解为什么在公开类型时会出错。在公开类型时,您将打开程序集以供其他程序集引用,这意味着它们可以看到其中的类型。

    如果您的类型声明为公共,并且它们具有公共构造函数,那么外部程序集应该可以调用它们的公共构造函数。正因为如此,构成构造函数的所有类型或程序集中某个类型的任何其他公共成员都必须具有公共可访问性。

    public class SomeClass
    {
        // This class and this constructor are externally visible
        // The parameter of type SomeOtherClass must also be public in order
        // for external assemblies to be able to construct this type
        public SomeClass(SomeOtherClass someOtherClass) { }
    }
    
    // This would cause the issue you are having since this type is private but
    // is included within a public contract (above), therefore the accessibility is 'inconsistent'
    private class SomeOtherClass { }
    

    不过我跑题了-你的问题是会员的可访问性

    您的静态成员 AppController 标记为private,意味着只能由 Form1 类(我假设这是它所在的类)

    解决方案(如Alex D所示)可以是 内部的 而不是 私有的 。这意味着该成员可以被同一程序集中的任何类型看到。 私有的 仅对声明成员的类型可见

    如果你成功了 可访问的( 平民的 )您将得到如上所示的错误。 内部的 保持程序集中的内部工作,这意味着您不会遇到这些可访问性问题。