为什么.net core允许通过项目依赖项访问程序集 [英] Why is .net core allowing assembly access through project dependencies

查看:213
本文介绍了为什么.net core允许通过项目依赖项访问程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在.net Framework项目/程序集中进行以下设置:

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标准库)参考rences Layer1

  • Layer1(.net标准库)没有引用

我能够这样,然后编译就可以了:

I am able to to this, and compile just fine:

其中对我来说毫无意义。

Which makes no sense to me.

这是设计使然吗?我该如何防止这种行为?

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="..\ClassLibraryA\ClassLibraryA.csproj" PrivateAssets="All" />  
  </ItemGroup>

另请参见此github问题

这篇关于为什么.net core允许通过项目依赖项访问程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆