C#/.NET 中仅命名空间的类可见性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223873/
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
Namespace-only class visibility in C#/.NET?
提问by nos
In C#, can you make a class visible only within its own namespace without living in a different assembly? This seems useful for typical helper classes that shouldn't be used elsewhere. (i.e. what Java calls package-private classes)
在 C# 中,是否可以使类仅在其自己的命名空间内可见,而无需位于不同的程序集中?这对于不应在其他地方使用的典型助手类似乎很有用。(即 Java 所谓的包私有类)
采纳答案by Steven Sudit
I don't think that what you want is possible.
我不认为你想要的是可能的。
回答by Robert Cartaino
You can make the classes internal
but this only prevents anyone outside of the assemblyfrom using the class. But you still have to make a separate assemblyfor each namespace that you want to do this with. I'm assuming that is why you wouldn'twant to do it.
您可以创建类,internal
但这只会阻止程序集之外的任何人使用该类。但是您仍然必须为要执行此操作的每个命名空间创建一个单独的程序集。我猜想这就是为什么你会不会想这样做。
Getting the C# Compiler to Enforce Namespace Visibility
让 C# 编译器强制命名空间可见性
There is an article here (Namespace visibility in C#) that shows a method of using partial classes as a form of "fake namespace" that you might find helpful.
这里有一篇文章(C# 中的命名空间可见性)展示了一种使用分部类作为“假命名空间”形式的方法,您可能会觉得这很有帮助。
The author points out that this doesn't work perfectly and he discusses the shortcomings. The main problem is that C# designers designed C# notto work this way.This deviates heavily from expected coding practices in C#/.NET, which is one of the .NET Frameworks greatest advantages.
作者指出这并不完美,他讨论了缺点。主要问题是 C# 设计者将 C# 设计为不以这种方式工作。这严重偏离了 C#/.NET 中的预期编码实践,这是 .NET Frameworks 的最大优势之一。
It's a neat trick… now don'tdo it.
这是一个巧妙的技巧......现在不要这样做。
回答by ShuggyCoUk
internal is assembly (strictly speaking module) privacy. It has no effect on namespace visibility.
内部是程序集(严格来说是模块)隐私。它对命名空间可见性没有影响。
The only way to achieve privacy of a class from other classes within the same assembly is for a class to be an inner class.
实现类与同一程序集中其他类的隐私的唯一方法是将类设为内部类。
At this point if the class is private it is invisible to anything not in that class or the outer class itself.
此时,如果该类是私有的,则该类或外部类本身之外的任何东西都是不可见的。
If protected it is visible to everyone that could see it when private but is also visible to sub classes of the outer class.
如果受保护,则对私有时可以看到它的每个人都可见,但对外部类的子类也可见。
public class Outer
{
private class Hidden { public Hidden() {} }
protected class Shady { public Shady() {} }
public class Promiscuous { public Promiscuous() {} }
}
public class Sub : Outer
{
public Sub():base()
{
var h = new Hidden(); // illegal, will not compile
var s = new Shady(); // legal
var p = new Promiscuous(); // legal
}
}
public class Outsider
{
public Outsider()
{
var h = new Outer.Hidden(); // illegal, will not compile
var s = new Outer.Shady() // illegal, will not compile
var p = new Outer.Promiscuous(); // legal
}
}
In essence the only way to achieve what you desire is to use the outer class as a form of namespace and restrict within that class.
本质上,实现您想要的唯一方法是使用外部类作为命名空间的一种形式并限制在该类中。
回答by Nathan
If you have a single assembly you can define as many namespaces in that assembly as you want but no matter what modifier you apply in the IDE you will always be able to see the classes in other namespaces.
如果您有一个程序集,您可以根据需要在该程序集中定义任意数量的命名空间,但无论您在 IDE 中应用什么修饰符,您都将始终能够看到其他命名空间中的类。
回答by Wyatt Barnett
Not sure if it is directly possible, but a few good ways to fake it would be:
不确定它是否直接可能,但有一些伪造它的好方法是:
1) Have the classes that need this sort of stuff inherit from a single class which has the helper class as an internal class.
1)让需要这类东西的类从一个类继承,该类将帮助类作为内部类。
2) Use extension methods and then only reference the extension methods within the namespace.
2) 使用扩展方法,然后只引用命名空间内的扩展方法。