C# 中的单例“无法访问”什么时候不在同一个命名空间中?

发布于 2024-10-14 00:21:02 字数 614 浏览 5 评论 0原文

我尝试通过以下方式实现单例类(我使用VS2008 SP1):

namespace firstNamespace
{
   class SingletonClass
   {
      private SingletonClass() {}

      public static readonly SingletonClass Instance = new SingletonClass();
   }
}

当我想从不同名称空间中的类访问它时(似乎这就是问题所在,在同一个名称空间中它可以工作),例如

namespace secondNamespace
{
   ...
   firstNamespace.SingletonClass inst = firstNamespace.SingletonClass.Instance;
   ...
}

:出现编译器错误:

error CS0122: 'firstNamespace.SingletonClass' is inaccessible due to its protection level

有人知道如何解决这个问题吗?

非常感谢!

I tried to implement a singleton class in the following way (I use VS2008 SP1) :

namespace firstNamespace
{
   class SingletonClass
   {
      private SingletonClass() {}

      public static readonly SingletonClass Instance = new SingletonClass();
   }
}

When I want to access it from a class in a different namespace (it seems that this is the problem, in the same namespace it works) like :

namespace secondNamespace
{
   ...
   firstNamespace.SingletonClass inst = firstNamespace.SingletonClass.Instance;
   ...
}

I get a compiler error:

error CS0122: 'firstNamespace.SingletonClass' is inaccessible due to its protection level

Does somebody have an idea how to solve this?

Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

↘人皮目录ツ 2024-10-21 00:21:03

您的 SingletonClass class 不是公共的,因此在命名空间 程序集之外不可见。

更正:评论是正确的,正如msdn

未嵌套在其他类或结构中的类和结构可以是公共的或内部的。声明为 public 的类型可由任何其他类型访问。 声明为内部的类型只能由同一程序集中的类型访问。类和结构默认声明为内部,除非将关键字 public 添加到类定义中,如上例所示。类或结构定义可以添加内部关键字以使其访问级别明确。访问修饰符不会影响类或结构本身 - 它始终可以访问其自身及其所有成员。

You SingletonClass class is not public, so isn't visible outside the namespace assembly.

Correction: the comments are right, as says msdn:

Classes and structs that are not nested within other classes or structs can be either public or internal. A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition, as in the previous example. Class or struct definitions can add the internal keyword to make their access level explicit. Access modifiers do not affect the class or struct itself — it always has access to itself and all of its own members.

聆听风音 2024-10-21 00:21:03

SingletonClass 具有内部可见性,因此如果两个命名空间位于不同的程序集中,则整个类将无法访问。

更改

class SingletonClass

public class SingletonClass

The SingletonClass has internal visibility, so if the two namespaces are in different assemblies, the entire class in inaccessible.

Change

class SingletonClass

to

public class SingletonClass
指尖微凉心微凉 2024-10-21 00:21:03

更改

class SingletonClass

public class SingletonClass

将其标记为公开,因此可访问

甚至更好:

public sealed class SingletonClass

,因为成员是静态的:

这里有更多内容

change

class SingletonClass

to

public class SingletonClass

to mark it public, thus accessible

or even better:

public sealed class SingletonClass

since the members are static:

more here

扬花落满肩 2024-10-21 00:21:03

您的类 SingletonClass 在其他命名空间中是可见的。但它在其他程序集/项目中不可见。

你的课程是私人的。这意味着当前项目 (=Assembly = .dll) 中的所有代码都可以看到此类。然而,该类对于其他项目中的代码是隐藏的。

命名空间和程序集之间的相关性很弱。一个命名空间可以存在于多个程序集中,例如 mscorlib.dll 和 System.dll 都包含 System 命名空间。

但通常情况下,当您在 Visual Studio 中创建新项目时,您会获得一个新的命名空间。

您还可以向一个程序集添加多个命名空间。当您创建新文件夹时,这会在 Visual Studio 中自动发生。

You class SingletonClass is visible in other namespaces. But it is not visible in other Assemblies/Projects.

Your class is private. This means all code in your current project (=Assembly = .dll) can see this class. The class is however hidden for code in other projects.

There is a weak correlation between the namespace and the assembly. One namespace can exist in multiple assemblies, for example mscorlib.dll and System.dll both contain the System namespace.

But normally, when you create a new project in Visual Studio, you get a new namespace.

You can also add multiple namespaces to the one Assembly. This happens automatically in Visual Studio when you create a new folder.

提赋 2024-10-21 00:21:03

您的类定义中缺少关键字public

You're missing the keyword public from your class definition.

满身野味 2024-10-21 00:21:03

听起来更像是单例位于不同的程序集中。类的默认修饰符是内部的,因此只能在程序集中访问。

Sounds more like the singleton is in a different assembly. The default modifier for a class is internal and thereby only accessible in the assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文