添加程序集引用也需要引用基础程序集 [英] Adding assembly reference requires base assembly to be referenced as well

查看:198
本文介绍了添加程序集引用也需要引用基础程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有子类的程序集,该子类派生自另一个程序集中定义的父级。

I created a assembly having a child class that derives from a parent defined in another assembly.

当我添加对子级的引用时,Visula studio也需要引用被添加到父级。

When I add reference to the child, Visula studio also requires reference to be added to the parent.

为什么会这样,如何在不丢失任何功能的情况下防止它发生?

Why is it so and how can I prevent it without losing any functionality?

推荐答案

在C / C ++中,.h头文件中存在类定义。这使您能够引用有关类的信息(例如,在需要从该类继承时,根据需要),而无需提供带有实现信息的文件。缺点是代码重复(.cpp文件中的实现需要重复.h文件中的大多数信息)。

In C/C++, class definition is present in a .h header file. That gives you ability to reference information about a class (as needed e.g. when you want to inherit from that class) without the need to source file with implementation information. The downside is code duplication (implementation in .cpp file needs to repeat most of the information in .h file).

.NET世界中的设计不同:组装包含类的代码(CLR字节码)以及例如所需的所有元数据(类名称,有关其成员的信息等)从该类继承。

In .NET world the design is different: an assembly contains both the code for the class (CLR bytecode) as well as all the metadata (class name, information about its members etc.) needed to e.g. inherit from that class.

该设计的结果是,为了使用在程序集A中定义的,从程序集B中的类继承的类,.NET既需要A和B程序集。或更笼统地说:如果您直接或间接使用给定程序集(类,枚举,结构)中的任何内容,则需要引用该程序集。

A consequence of that design is that in order to use a class defined in assembly A that inherits from a class in assembly B, .NET needs both A and B assemblies. Or more generically: if you use anything from a given assembly (a class, an enum, a struct), either directly or indirectly, you need to reference that assembly.

我不确定您要阻止什么。如果您决定按照上述方式将代码拆分为两个程序集,那么就没有必要同时引用这两个程序集。

I'm not sure what you want to prevent. If you decide to split your code in two assemblies like you described, there's no way around the need to reference both of them.

当然,可以使用不同的方式构造代码但不知道首先将代码分成2个程序集所要达到的目标,因此无法提出有用的建议。

There are, of course, different ways of structuring your code but not knowing what goal you're trying to achieve by splitting the code into 2 assemblies in the first place, it's impossible to make a useful suggestion.

这篇关于添加程序集引用也需要引用基础程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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