I have the following scenario of project references in a C# solution, such that:
- Project A references Project B
- Project A references Project C
- Project B references Project C
This solution compiles fine.
Now I remove the reference from Project A to Project C and it still compiles! It's like Project A is indirectly referencing Project C though Project B, i.e.:
- Project A references Project B
- Project B references Project C
Is that how project referencing works? Now in Project A I can mysteriously use classes etc from Project C indirectly through Project B.
I'm developing with Visual Studio 2022 on Windows 11 with projects targeting .Net Framework 4.7.2
This isn't just a case of weirdly working on a local machine, as the same solution also compiles as part of a Jenkins CI/CD pipeline.
I have the following scenario of project references in a C# solution, such that:
- Project A references Project B
- Project A references Project C
- Project B references Project C
This solution compiles fine.
Now I remove the reference from Project A to Project C and it still compiles! It's like Project A is indirectly referencing Project C though Project B, i.e.:
- Project A references Project B
- Project B references Project C
Is that how project referencing works? Now in Project A I can mysteriously use classes etc from Project C indirectly through Project B.
I'm developing with Visual Studio 2022 on Windows 11 with projects targeting .Net Framework 4.7.2
This isn't just a case of weirdly working on a local machine, as the same solution also compiles as part of a Jenkins CI/CD pipeline.
Share Improve this question asked 2 days ago Simon BosleySimon Bosley 1,1543 gold badges20 silver badges42 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 5This is expected - the terminology is that "C is a transitive dependency of A".
It can be logically broken down like this:
- If project B references - i.e., includes the code of - project C...
- And project A references/includes the code of project B...
- Then project A must also include the code from project C.
If, for some reason, you want A to be unable to see C, you can set PrivateAssets
to "all" while declaring B's dependency on C - see docs.
<ItemGroup>
<!-- ... -->
<ProjectReference Include="C.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<!-- ... -->
</ItemGroup>
PrivateAssets="All"
on theProjectReference
, see here – canton7 Commented 2 days ago