C# 中的单例“无法访问”什么时候不在同一个命名空间中?
我尝试通过以下方式实现单例类(我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的 SingletonClass class 不是公共的,因此在
命名空间程序集之外不可见。更正:评论是正确的,正如msdn:
You SingletonClass class is not public, so isn't visible outside the
namespaceassembly.Correction: the comments are right, as says msdn:
SingletonClass 具有内部可见性,因此如果两个命名空间位于不同的程序集中,则整个类将无法访问。
更改
为
The SingletonClass has internal visibility, so if the two namespaces are in different assemblies, the entire class in inaccessible.
Change
to
更改
为
将其标记为公开,因此可访问
甚至更好:
,因为成员是静态的:
这里有更多内容
change
to
to mark it public, thus accessible
or even better:
since the members are static:
more here
您的类
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.
您的类定义中缺少关键字
public
。You're missing the keyword
public
from your class definition.听起来更像是单例位于不同的程序集中。类的默认修饰符是内部的,因此只能在程序集中访问。
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.