Reputation: 2251
I have a project called Category_Helper_Project
.
Within this project are two relevant classes: Category_Interface
and 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
. Functions in Category_File_Manager
are public for this reason.
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_File_Manager` directly most likely causes problems in the tree structure.
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.
I have found some simmilar questions such as Making a function only accessible from one other function but they didnt really answer my questions.
Upvotes: 0
Views: 942
Reputation: 434
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
Upvotes: 0
Reputation: 12619
To control class accessibility you have a keyword you add before its declaration:
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.
You can circumvent this using InternalsVisibleTo assembly level attribute for test library benefit.
[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
Upvotes: 3