为什么 .net core 允许通过项目依赖项访问程序集 [英] Why is .net core allowing assembly access through project dependencies
问题描述
在 .net 框架项目/程序集中考虑这个设置:
Consider this setup in .net framework projects/assemblies:
- layer3(.net 框架控制台应用程序)引用 Layer2
- Layer2(.net 框架库)引用 Layer1
- Layer1(.net 框架库)不引用任何内容
现在当我尝试从 Layer3 访问 Layer1 时,它不允许我:
Now when I try to access Layer1 from Layer3, it won't allow me:
找不到类型或命名空间名称Layer1"(您是否缺少 using 指令或程序集引用?)
The type or namespace name 'Layer1' could not be found (are you missing a using directive or an assembly reference?)
这是有道理的.
现在,当我在 .net 标准项目和 .net 核心控制台应用程序中创建相同的设置时,如下所示:
Now when I create the same setup in .net standard projects and a .net core console app, like so:
- layer3(.net 核心控制台应用程序)引用 Layer2
- Layer2(.net 标准库)引用 Layer1
- Layer1(.net 标准库)不引用任何内容
我能够做到这一点,并且编译得很好:
I am able to to this, and compile just fine:
这对我来说毫无意义.
这是故意的吗?如何防止这种行为?
Is this by design? How do I prevent this behaviour?
推荐答案
所以,事实证明这是设计使然,解决方法是这样的:
So, turns out this is by design and the workaround is this:
<ItemGroup>
<ProjectReference Include="..ClassLibraryAClassLibraryA.csproj" PrivateAssets="All" />
</ItemGroup>
这篇关于为什么 .net core 允许通过项目依赖项访问程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!