3
  • ProjectA references ProjectB
  • ProjectB references ProjectC
  • ProjectA has <RestoreProjectStyle>PackageReference</RestoreProjectStyle> in the .csproj

Given the above, it is possible to directly reference ProjectC types in ProjectA. This is undesirable since I don't want to leak types into ProjectA.

How can I avoid this compile-time automatic dependency inheritance / type exposure?

Palec
  • 12,743
  • 8
  • 69
  • 138
Max
  • 39
  • 1
  • 4

1 Answers1

1

You can't really at the moment. There are some workarounds like trying to change the references to a different namespace etc.

If you don't want some types to be instantiated by other assemblies, consider using internal constructors/types (along with InternalsVisibleTo assembly attributes for testing) or Roslyn analyzers to enforce usage rules.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • Thanks for confirming the issue! Do you have a link to somewhere with more information about this? Or do you happen to know if there is a change planned to address this? I cannot actually change anything in ProjectC, since in my case that is actually Microsoft.Extensions.Logging. – Max May 24 '18 at 07:15
  • no, there is no plan. if the project in question would be the application hosting everything it would have needed a project reference as well, thus exposing all types. so the compiler now at least sees everything that's involved and the build system can resolve conflicts – Martin Ullrich May 24 '18 at 07:24
  • What do you need links to? `InternalsVisibleTo`? – Martin Ullrich May 24 '18 at 07:25
  • 1
    A link to something discussing this change. Please correct me if I'm wrong, but isn't a reference to ProjectC from the hosting application unnecessary, since assembly resolving would load the necessary assemblies without actually exposing the types at compile time? So at runtime the types would be loaded correctly, but ProjectA (the hsoting application) couldn't make explicit references to the types in code – Max May 24 '18 at 08:12
  • ... As long as none of the types in ProjectC are exposed in the public interface of ProjectB, of course. – Max May 24 '18 at 08:14