[英]Making functions accessible from other classes in same project but not in another Project
I have a project called Category_Helper_Project
.我有一个名为
Category_Helper_Project
的项目。
Within this project are two relevant classes: Category_Interface
and Category_File_Manager
在这个项目中有两个相关的类:
Category_Interface
和Category_File_Manager
The purpose of this project is that it can be referenced from other projects.这个项目的目的是它可以被其他项目引用。 It handles the underlying file system automatically when creating/accessing categories.
它在创建/访问类别时自动处理底层文件系统。
To do that, class Category_Interface
accesses class Category_File_Manager
.为此, class
Category_Interface
访问 class Category_File_Manager
。 Functions in Category_File_Manager
are public for this reason.由于这个原因,
Category_File_Manager
中的函数是公开的。
If I reference Category_Helper_Project
from another Project, is it possible to make Category_File_Manager
inaccessible from the other project whilst keeping it accessible from Category_Interface? Accessing
如果我从另一个项目中引用
Category_Helper_Project
,是否可以使Category_File_Manager
无法从另一个项目中访问,同时又可以从Category_Interface? Accessing
Category_Interface? Accessing
Category_File_Manager` directly most likely causes problems in the tree structure.直接
Category_Interface? Accessing
Category_File_Manager 很可能会导致树结构出现问题。
Lastly I would like to know if making Category_File_Manager
inaccessible will also make it inaccessible in unit tests, which would be quite unfortunate.最后,我想知道是否使
Category_File_Manager
不可访问也会使其在单元测试中不可访问,这将是非常不幸的。
I have found some simmilar questions such as Making a function only accessible from one other function but they didnt really answer my questions.我发现了一些类似的问题,例如使 function 只能从另一个 function 访问,但他们并没有真正回答我的问题。
To control class accessibility you have a keyword you add before its declaration:要控制 class 可访问性,您需要在声明之前添加一个关键字:
public class Category_File_Manager
If you do not add the public
it defaults to internal
and that would result in class being accessible from project/library that declares it and not accessible from any other, that includes tests.如果您不添加
public
,则默认为internal
,这将导致 class 可以从声明它的项目/库访问,并且不能从任何其他访问,包括测试。
You can circumvent this using InternalsVisibleTo assembly level attribute for test library benefit.您可以使用InternalsVisibleTo程序集级别属性来规避此问题,以获得测试库的好处。
[assembly: InternalsVisibleToAttribute("Category_Helper_Project_Tests")]
This has a bit of a code smell that your library becomes somewhat aware of the test library but this is a case of having a cake and eating it.这有一点代码味道,您的库会有点意识到测试库,但这是一个吃蛋糕并吃掉它的情况。 It is a good idea to remove such access from production code so I would wrap this in compilation if so it is not added on release build.
从生产代码中删除此类访问是一个好主意,因此如果未在发布版本中添加它,我会将其包装在编译中。
#if DEBUG
[assembly: InternalsVisibleToAttribute("Category_Helper_Project_Tests")]
#endif
as i have read your question over and over I reckon that you want to hide a class to other projects and also make it accessible from the inside of Category_File_Manager
this cannot be done with one access modifier you should simply make Category_Interface
internal to hide it from outside and make another class with only needed APIs to pass out from inside of Category_File_Manager
More You can read here about access modifiers当我一遍又一遍地阅读您的问题时,我认为您想将 class 隐藏到其他项目,并使其可以从
Category_File_Manager
内部访问,这不能通过一个访问修饰符来完成,您应该简单地将Category_Interface
设为内部以将其从外部隐藏并制作另一个 class 只需要从Category_File_Manager
内部传递的 API 更多您可以在此处阅读有关访问修饰符的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.