简体   繁体   English

使函数可以从同一项目中的其他类访问,但不能在另一个项目中访问

[英]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_InterfaceCategory_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.

相关问题 从另一个项目访问内部类 - Access to internal classes from another project 如何从另一个项目中的一个项目调用类? - How to call classes from one Project in another Project? 无法访问其他项目中的公共类,但访问相同的命名空间 - Can't access public classes in an other project but the same namespace 在同一解决方案中自动完成来自另一个项目的课程不起作用 - Auto completion of classes from another project in the same solution doesn't work 在同一解决方案中从另一个项目中的一个项目打开xaml页面 - Open a xaml page from one project in another project in the same solution 在同一解决方案中将一个项目的照片文件用于另一个项目 - Use photo file from one project to another project in the same solution 将服务引用从一个项目复制到同一解决方案中的另一个项目 - Copy service reference from one project to another project in same solution 在同一解决方案中使用另一个项目的引用 - Use reference from another project at the same solution 绑定到同一解决方案中另一个项目的属性 - Bind to property from another project in same solution 读取变量-从位于项目其他文件夹中的类 - Reading variables - from classes that are located in other folders in project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM