简体   繁体   English

如何将所有公共方法转换为内部方法?

[英]How to turn all public methods to internal methods?

Before deploying my project, I would like to set all public methods to internal methods. 在部署项目之前,我想将所有public方法设置为internal方法。 Does someone know a built-in function in Visual Studio or an external tool to do such tasks? 有人知道Visual Studio中的内置功能或执行此任务的外部工具吗?

With some trivial refactoring, ILMerge can work here. 通过一些简单的重构, ILMerge可以在这里工作。 ILMerge can merge multiple assemblies into one, and change the accessibility of everything that is not part of the primary assembly to internal . ILMerge可以将多个程序集合并为一个,并将不属于主程序集的所有内容的可访问性更改为internal By separating your current project into two projects (a library containing all the code, and a helper executable that does nothing but forward to the real code in the library), you can, after a build, merge them back into a single file, in which all the library bits are no longer public. 通过将您的当前项目分为两个项目(一个包含所有代码的库,以及一个仅可执行到库中实际代码的帮助程序可执行文件),可以在构建后将它们合并回一个文件中,所有的库位不再公开。

If you have properties/methods public for test reasons you should have a look at Brad Wilsons blog: Testable Object Pattern 如果出于测试原因而拥有公共的属性/方法,则应查看Brad Wilsons博客: 可测试对象模式

This way you don't have to switch, everything stays internal in development. 这样,您无需切换,一切都在开发内部。

Or have a look at Jon Skeets suggestion on InternalsVisibleTo 或者看看Jon Skeets在InternalsVisibleTo上的建议

I can only think of two reasons you would want to do this: 我只能想到您要这样做的两个原因:

  • Security. 安全。 In this case you have the wrong idea: access modifiers are not a security mechanism. 在这种情况下,您有一个错误的主意:访问修饰符不是安全机制。 They are a design concern, describing how an API presents itself. 它们是设计方面的问题,描述了API如何呈现。 Any debugger will still give access to all your methods, regardless of their access modifiers. 任何调试器仍将授予您所有方法的访问权限,而不管其访问修饰符如何。

  • You have another assembly that you use during development that should have full access. 您在开发期间使用的另一个程序集应具有完全访问权限。 In this case you can leave everything internal and make it a friend assembly . 在这种情况下,您可以将所有内容保留在internal并使其成为好友程序集

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM