1

I have 3 projects in the Solution.

  • TestProject
  • Child
  • childOfchild

-TestProject has reference to Child project

-Child project has reference to childOfchild project

I would expect not to be able to create an instance of the class from the childOfchild project inside the TestProject. It does not make sense to me. If I want I will import childOfchild project to TestProject but I don't want that. This is exactly what I want to avoid and I want clearly separate layers.

enter image description here

I am quite sure that this did not work like this earlier. I would appreciate if someone explains me why is this case.

Raskolnikov
  • 3,791
  • 9
  • 43
  • 88
  • 1
    Isn't it because it refers a project of the same solution? at runetime we can't avoid the test project to call the child of child. But anyway I also have a feeling this may be something I would like to avoid, at least in my VS solution when compilation happens. – Guillaume S. Dec 26 '19 at 09:22

1 Answers1

2

This behavior is provided by the new project system for .NET Core/.NET Standard/SDK-based csproj (pick a name, they all mean the same). The new behavior is that PackageReferences and ProjectReferences are now transitive by default, meaning that when you reference a project, you will automatically inherit its Package- or ProjectReferences.

You can control this on the project which contains that reference by setting the PrivateAssets property on that reference. In your example: in Child.csproj, you can do this:

<ItemGroup>
  <ProjectReference Include="..\ChildOfChild\ChildOfChild.csproj" PrivateAssets="all" />
</ItemGroup>

This tells the project system "I want to reference ChildOfChild, but I don't want that implementation detail exposed to projects which reference me."

See this blog post for more information.

Kit
  • 20,354
  • 4
  • 60
  • 103
Jimmy
  • 27,142
  • 5
  • 87
  • 100
  • Adding this: the rules about what can be referenced in an assembly (e.g. public classes) haven't changed, just how you get to them transitively... – Kit Dec 26 '19 at 18:31