2

I have this dependency of projects:

Client [.NET 4.7.2] -> Infrastructure [.NET Standard 2.0] -> ExternalLib [dll]

When I try to call a method of Infrastructure that uses ExternalLib from the Client project, the compiler generates the following error:

Error CS0012 The type ClassX is defined in an assembly that is not referenced. You must add a reference to assembly ExternalLib

Is it possible to do it implicitly? I mean it shouldn't be needed to add this reference explicitly since it is already done in the Infrastructure project.

Thank you

Liam
  • 27,717
  • 28
  • 128
  • 190
Gofu
  • 51
  • 8

1 Answers1

8

I believe when you initially said "recursive" you really meant "transitive" (as per your recent edit).

.NET, and .NET framework in particular doesn't really handle transitive references. It, as you have found, requires all dependencies to be explicitly referenced by the executing projects. An "implicit" reference isn't going to happen.

There seems to be better support for pulling in transitive dependencies in .NET Core, at least if the primary dependency is a Nuget package.

mjwills
  • 23,389
  • 6
  • 40
  • 63
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Considering IntelliSense and the type resolution during compilation seems to scan all references to know wich classes it has to autocomplete/wich ones are meant, such an automatic would propably backfire horribly and with a humongous time wasted. So there is even a really good reason not to do that. – Christopher Oct 17 '19 at 21:59
  • Thank you for your answers. I just thought that there could be a way that MSBuild could check that the Infrastructure project has the reference to ExternalLib and add it to the Client project as well. It does not seem such a huge workload. – Gofu Oct 17 '19 at 22:14
  • @Gofu It will do that... for a nuget package. For whatever reason the designers for .NET Framework (again, Core seems to handle this better) decided not to. – BradleyDotNET Oct 17 '19 at 22:22
  • @BradleyDotNET even for .NET Core it would only work with nuget right? – Gofu Oct 17 '19 at 22:24
  • @Gofu I can't say I've done enough multi-project Core projects to say for certain. I know the ProjectReference mechanic works a little different than Framework references do. – BradleyDotNET Oct 17 '19 at 22:26
  • I will give it a try one of this days. Thanks for your help and for your time @BradleyDotNET ! – Gofu Oct 17 '19 at 22:30
  • 1
    @Gofu, I checked it on a .NET Core repro project and indeed transitive references dont need to the added directly to uppermost project to be able to directly use one of its types. – Sam Bauwens Oct 01 '20 at 10:56