C# 在类库中隐藏仅包含内部类型的命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720647/
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
Hiding namespaces containing only internal types in a class library?
提问by Lasse V. Karlsen
I have a class library that has a couple of namespaces containing only internal types.
我有一个类库,它有几个只包含内部类型的命名空间。
However, when using the class library in an application project, the namespaces shows up in intellisense, but of course they are empty. Is there any way for me to hide the namespaces completely when using intellisense in other projects?
但是,在应用程序项目中使用类库时,命名空间在智能感知中显示,但当然它们是空的。在其他项目中使用智能感知时,我有什么办法可以完全隐藏命名空间?
I've tried to apply EditorBrowsableAttribute
to all the internal classes as well, but what I'd like to do would be to apply that to the namespace, which is of course impossible.
我也尝试将其应用于EditorBrowsableAttribute
所有内部类,但我想做的是将其应用于命名空间,这当然是不可能的。
Or is, if I care enough about this, the only option I have to just move the types into a namespace that contains public types?
或者,如果我对此足够关心,我必须将类型移动到包含公共类型的命名空间中的唯一选择?
采纳答案by takrl
It depends on how you're referencing your class library:
这取决于您如何引用您的类库:
- If you have the class library project contained within your solution and use a project reference, you'll always see that empty namespace via Intellisense.
- If you're referencing a compiled dll of your class library, you won't see the namespace popping up in intellisense, provided it contains only internal members.
- 如果您的解决方案中包含类库项目并使用项目引用,您将始终通过 Intellisense 看到该空命名空间。
- 如果您正在引用类库的已编译 dll,您将不会看到在智能感知中弹出的命名空间,前提是它只包含内部成员。
Try this:
尝试这个:
namespace ClassLibrary1
{
namespace Internal
{
internal class InternalClass
{
public int internalStuff { get; set; }
}
}
namespace Public
{
public class PublicClass
{
public int publicStuff { get; set; }
}
}
}
If you reference this via a project reference, you'll see the empty namespace. If you reference a dll of it, you won't.
如果您通过项目引用来引用它,您将看到空命名空间。如果您引用它的 dll,则不会。
回答by James L
I've come up against this before and found no solution, only a workaround which may or may not work in your project. Instead of defining a namespace you could use an nested static class?
我以前遇到过这个问题,但没有找到解决方案,只有一种在您的项目中可能有效也可能无效的解决方法。您可以使用嵌套的静态类而不是定义命名空间?