0

I have two .Net Standard 2.0 library projects: Lib1 and Lib2, Lib2 depends on Lib1, Lib1 has class Class1, Lib2 has class Class2. Then I create a .Net Core 2.0 and a .Net Framework project:

  • The .Net Core 2.0 console application project named DevInNetCore which only depends on Lib2, but Lib1.Class1 can be accessed in this project.
  • The .Net Framework 4.6.1 console application project named DevInNetFramework which also only depends on Lib2, but now Lib1.Class1 can't be accessed in this project.

My question is: Why Lib1.Class1 can be accessed in the .Net Core project DevInNetCore since it only depends on project Lib2? Is there any way to let Lib1 can not be accessed in the project DevInNetCore?

Here is my demo solution: https://www.dropbox.com/s/u60xjl3i37arcs2/LibTest.zip?dl=0, Visual Studio 2017 may be needed to load the solution.

Dallon Xu
  • 80
  • 5

1 Answers1

0

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

This change came with new format of csproj files. Here is related issue on github. If you check the references in Visual Studio, you will see that for .Net Core project VS 'knows' that Lib2 depends on Lib1. However this is not the case for reference from .Net Framework project.

enter image description here

So answering your question "Why ..." - because it's the feature that was implemented for new project format and not for the legacy one.

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

Here is related discussion on github. I agree with Eilon's comment that such restriction should not take a place in general. However if you still want to do it, you could make types of Lib1 internal and then allow access them from Lib2 with InternalsVisibleTo attribute.

CodeFuller
  • 30,317
  • 3
  • 63
  • 79