是否有限制使用解决方案的访问修饰符? [英] Is there an access modifier that limits to a solution?

查看:68
本文介绍了是否有限制使用解决方案的访问修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.NET解决方案中,我有两个项目:一个主项目和一个针对主项目运行测试的项目。在我的项目中,我有几种方法可以保持私有,但也要对其进行测试。是否有访问方法可以将这些功能限制在我的解决方案中?

In my .NET solution, I have two projects: one main project and a project for running tests against the main project. In my project, I have several methods that I'd like to keep "private", but would also like to run tests for. Is there an access method that could limit these functions to just inside of my solution?

推荐答案

您正在寻找 InternalsVisibleTo 属性。

此属性使您可以指定其他程序集,这些程序集应有权访问程序集内部的类型和方法。因此,在您的主项目AssemblyInfo.cs文件(或任何其他源文件)中,您可以指定您的测试项目是朋友程序集,并且应该可以访问主项目的内部:

This attributes lets you specify other assemblies that should have access to types and methods that are internal to your assembly. So, in your main project AssemblyInfo.cs file (or any other source file), you can specify that your test project is a 'friend assembly' and should have access to the internals of your main project:

[assembly:InternalsVisibleTo("MainProject.Tests")]

在旁注中,正如Alexei所指出的,如果MainProject是使用强名称键签名的,则任何朋友程序集也必须签名。在此处进行解释

On a side note, as pointed out by Alexei, if your MainProject is signed with a strong name key, any 'friend' assembly must also be signed. This is explained here

尽管,如另一条评论中所述。最佳做法是使用程序集的公共API测试程序集。

Although, as mentioned in another comment. Best practice is to test your assembly by using its public API.

这篇关于是否有限制使用解决方案的访问修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆