Namespace-only class visibility in C#/.NET?

前端 未结 5 1834
温柔的废话
温柔的废话 2020-12-09 14:45

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 use

5条回答
  •  有刺的猬
    2020-12-09 15:07

    You can make the classes internal but this only prevents anyone outside of the assembly from using the class. But you still have to make a separate assembly for each namespace that you want to do this with. I'm assuming that is why you wouldn't want to do it.

    Getting the C# Compiler to Enforce Namespace Visibility

    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.

    The author points out that this doesn't work perfectly and he discusses the shortcomings. The main problem is that C# designers designed C# not to work this way. This deviates heavily from expected coding practices in C#/.NET, which is one of the .NET Frameworks greatest advantages.

    It's a neat trick… now don't do it.

提交回复
热议问题