.Net Core项目在依赖其他项目时访问过多class或API

.Net Core project accesses too many class or API when it depends on some other projects

我有两个 .Net Standard 2.0 库项目:Lib1Lib2Lib2 取决于 Lib1Lib1 有 class Class1Lib2有classClass2。然后我创建一个 .Net Core 2.0 和一个 .Net Framework 项目:

我的问题是:为什么Lib1.Class1可以在.Net Core项目DevInNetCore中访问,因为它只依赖于项目Lib2?有没有办法让Lib1在项目DevInNetCore中无法访问?

这是我的演示解决方案:https://www.dropbox.com/s/u60xjl3i37arcs2/LibTest.zip?dl=0Visual Studio 2017 可能需要加载解决方案。

Why Lib1.Class1 can be accessed in the .Net Core project DevInNetCore since it only depends on project Lib2?

这一变化伴随着 csproj 文件的新格式。这是 github 上的 related issue。如果您检查 Visual Studio 中的引用,您将看到对于 .Net Core 项目 VS 'knows',Lib2 依赖于 Lib1。然而,.Net Framework 项目的参考并非如此。

所以回答你的问题 "Why ..." - 因为这是为新项目格式而非旧项目格式实现的功能。

Is there any way to let Lib1 can not be accessed in the project DevInNetCore?

这里 related discussion on github. I agree with Eilon's comment 这样的限制通常不应该出现。但是,如果您仍想这样做,可以将 Lib1 类型设为内部类型,然后允许使用 InternalsVisibleTo 属性从 Lib2 访问它们。