如何避免引用dll的引用的DLL [英] How to to avoid referencing a dll's referenced dll

查看:125
本文介绍了如何避免引用dll的引用的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net项目(ProjA)和一个类库项目(LibB)。我在库中创建了一个自定义类(ClassC),并添加了对现有dll(DllD)的引用。



我在ProjA中从解决方案选项卡中选择了添加参考选项我选择了LibB并添加了引用。



在ProjA中,我创建了一个ClassC的实例。令我惊讶的是,它没有编译,并给我一个错误:DllD必须被添加为我的ProjA的参​​考。



这是消息:


类型XXXX未引用的程序集。你
必须添加对程序集YYYYYY,Version = 1.0.0.0,
Culture = neutral,PublicKeyToken = xxxxxxx的引用。


嗯,XXX是ClassC的基础,在LibB项目中引用了dll。



那么LibB有什么意义?我不想在我的ProjA项目中添加DllD,我只想在另一个项目中使用依赖于它的自定义类



这是正常的,我必须添加这样的dll?
我应该设置什么来使我的LibB包含DllD dll?

解决方案

问题是, LibB 必须将 DllD 中包含的某些类型暴露给您的代码 ProjA 1



如果这些类型在幕后消耗,那么引用不是必需的。但这显然不是什么。所以编译器希望引用能够完全了解 LibB 中的类型和成员。






根据您的编辑:


嗯,XXX是ClassC和dll的基础在LibB项目中引用。


是的,编译器需要了解该类,包括所有的基类。如果,而不是继承,则使用了组合,而 ClassC 只包含对 XXX 实例的私有引用,引用不需要。






1 最明显的方法是:

  public class ClassC {
void DoSomething(SomeTypeFromDllD input){...}
}


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?

解决方案

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){...}
}

这篇关于如何避免引用dll的引用的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆