如何从父项目中隐藏类库
我们有一个带有 .NET Core C# API 项目的解决方案。 API 项目依赖于类库。类库具有 API 项目不需要了解的依赖项。为了更容易可视化,我正在讨论以下内容:
问题:如何从 API 项目中隐藏一些类库,同时仍使它们可供使用类库直接调用来自 API 项目?目标是确保 API 项目不会直接调用它使用的类库的子依赖项。
失败的方法:
- 使用“内部” - 不起作用 - “父”类库和依赖类库都是不同的程序集,即父类库不能使用内部类库。
- 使用“受保护” - 不起作用,任何类继承最多都被硬塞在这里
- 对类库及其依赖项使用完全不同的解决方案,将其导入为 NuGet 包 - 对于积极工作的代码库来说工作量太大
We have a Solution with a .NET Core C# API project. The API project relies on a Class Library. The Class Library has dependencies that the API project needs to know nothing about. To make it easier to visualize, here is what I am talking about:
Question: How do I hide some class libraries from the API project, WHILE still making them available to the Class Library called directly from the API project? The goal is to make sure the API project doesn't directly call sub-dependencies of the Class Library it uses.
Failed approaches:
- Use "internal" - doesn't work - the "parent" and dependent class libraries are all different assemblies, i.e. the parent Class Library can't use dependent Class Libraries if they are internal.
- Use "protected" - doesn't work, any class inheritance is shoehorned in here at best
- Use an entirely different solution for the Class Library and its dependencies, import it as NuGet package - too much work for an actively worked-on codebase
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以通过修改
*.csproj
文件中默认的ProjectReference
部分来隐藏继承的类库的访问。假设解决方案结构如下所示:
要隐藏
API
项目中的SubClassLibray
程序集,我们需要修改ClassLibrary.csproj
文件如下:We can hide the inherited class libraries' access by modifying the default
ProjectReference
section in the*.csproj
file.Let's say the solution structure looks like this:
To hide the
SubClassLibray
's assemblies in theAPI
project, we need to modify theClassLibrary.csproj
file as below: