简体   繁体   English

“内部”成员的细粒度可见性

[英]Fine-grained visibility for 'internal' members

Recently I was reading about partitioning code with .NET assemblies and stumbled upon a nice suggestion from this post : "reduce the number of your .NET assemblies to the strict minimum". 最近我读到有关分区与.NET程序集的代码和偶然从一个很好的建议, 这个帖子 :“减少你的.NET程序集的数量进行严格的最低限度”。

I couldn't agree more! 我不能同意! And one of the reasons I personally see most often, is that people just want to isolate some piece of code, so they make the types/methods internal and put them into a separate project. 我个人经常看到的原因之一是,人们只想隔离一些代码,因此他们将类型/方法内部化并将它们放入一个单独的项目中。

There are many other reasons (valid and not) for splitting code into several assemblies, but if you want to isolate components/APIs while still having them located in one library, how can you do that? 将代码拆分为多个程序集还有许多其他原因(有效和无效),但是如果要将组件/ API隔离在一个库中,那么如何隔离组件/ API,那么如何做到这一点呢?

namespace MyAssembly.SomeApiInternals
{
     //Methods from this class should not 
     //be used outside MyAssembly.SomeApiInternals
     internal class Foo
     {              
          internal void Boo() { }
     }
}

namespace MyAssembly.AnotherPart
{
     public class Program
     {              
          public void Test() 
          {
               var foo = MyAssembly.SomeApiInternals.Foo();
               foo.Boo(); //Ok, not a compiler error but some red flag at least 
          }
     }
}             

How can one restrict a type/method from being used by other types/methods in the same assembly but outside this very namespace? 如何限制类型/方法在同一个程序集中被其他类型/方法使用但在此命名空间之外?

(I'm going to give a few answers myself and see how people would vote.) (我将自己给出一些答案,看看人们会如何投票。)

Thanks! 谢谢!

您可以将代码放在不同的程序集中,然后在构建后的步骤中将程序集与ILMerge合并......

Use NDepend and put in CQL rules that embody what you want and run them as part of your build. 使用NDepend并输入体现您想要的CQL规则并将其作为构建的一部分运行。 The language isnt interested in this level of restrictions. 该语言对此级别的限制不感兴趣。 (I hadnt followed your link yet - are you really trying to do this without NDepend? Your answer should rule it in or out) (我还没有关注你的链接 - 你是否真的试图在没有NDepend的情况下做到这一点?你的答案应该在其中进行规则)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#细粒度权限:多个内部系统,Intranet方案 - C# fine-grained permissions: multiple internal systems, intranet scenario C#中的细粒度内存分析 - Fine-grained memory profiling in C# 对横向扩展应用细粒度控制 - applying fine-grained control over scale out 细粒度的权限; PrincipalPermission-角色与权限分开; - fine-grained permissions; PrincipalPermission - roles seperate from permissions; 在asp.net成员中实现细粒度的运行时权限? - Implementing fine-grained runtime permissions in asp.net Membership? 实现IQueryable时对延迟执行的细粒度控制<T> - Fine-grained control of deferred execution when implementing IQueryable<T> 最佳实践缓存:单片与细粒度缓存数据 - Best-practice caching: monolithic vs. fine-grained cache data 使用Excel和.NET进行细粒度的单元着色 - Fine Grained Cell Coloring with Excel and .NET 共享项目列表的细粒度同步 - Fine grained synchronization of a shared item list 使用Subsonic的SimpleRepository进行细粒度CRUD - Fine Grained CRUD with Subsonic's SimpleRepository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM