How to to avoid referencing a dll's referenced

2019-07-20 20:24发布

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?

标签: c# dll
1条回答
Summer. ? 凉城
2楼-- · 2019-07-20 20:37

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){...}
}
查看更多
登录 后发表回答