2

I have an ASP.Net project (ProjA) and a class library project (LibB). I created a custom class (ClassC) in the library and added references to an existing dll (DllD).

I chose the Add reference option in ProjA and from the Solution tab I selected LibB and added the reference.

In ProjA I created an instance of ClassC. To my surprise, it didn't compile and showed me an error: DllD must be added as a reference to my ProjA.

Here is the message:

The type 'XXXX' is defined in an assembly that is not referenced. You must add a reference to assembly 'YYYYYY, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxx'.

Well, XXX is a class that is the base of ClassC and the dll is referenced in the LibB project.

Then what's the point of LibB? I don't want to add DllD to my ProjA project, I just want to use a custom class that depends on it in another project.

Is it normal that I have to add dlls like that? What should I set to make my LibB contain the DllD dll?

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Nestor
  • 8,194
  • 7
  • 77
  • 156

1 Answers1

6

The problem is, LibB must be exposing some of the types contained in DllD to your code in ProjA.1

If it just consumed those types behind the scenes, then the reference wouldn't be necessary. But that's not, apparently, what it does. So the compiler wants a reference to be able to fully understand the types and members in LibB.


Based on your edit:

Well, XXX is a class that is the base of ClassC and the dll is referenced in the LibB project.

Yes, the compiler needs to understand the class, including all of it's base classes. If, instead of inheritance, you used composition, and ClassC just contained a private reference to an XXX instance, the reference wouldn't be required.


1The most obvious way would be:

public class ClassC {
    void DoSomething(SomeTypeFromDllD input){...}
}
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Define consumed please. SomeTypeFromDllD is inside the LibB project. It should contain all the necessary metadata. Why do I have to provide addtional reference to it? – Nestor Apr 23 '14 at 14:13
  • So, to be clear, if I use *inheritance*, I cannot avoid adding the reference, right? – Nestor Apr 23 '14 at 14:23
  • @Nestor - I would say not. Consider that this base class may define e.g. conversion operators that the compiler might now be interested in. – Damien_The_Unbeliever Apr 23 '14 at 14:24
  • Yes, that's a fair assumption. Thanks for the clarification. – Nestor Apr 23 '14 at 14:26