添加程序集引用还需要引用基础程序集
我创建了一个程序集,该程序集具有一个子类,该子类派生自另一个程序集中定义的父类。
当我添加对子项的引用时,Visula studio 还要求将引用添加到父项。
为什么会这样?如何在不丢失任何功能的情况下防止它?
I created a assembly having a child class that derives from a parent defined in another assembly.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所描述的情况是部分可能的。您可以消除它们显式引用隐藏程序集的需要,但该程序集仍然会在编译时被拉入,并在运行时需要。
假设您定义了这些类:
如果最终用户使用类 B,他们将需要对程序集 1 的显式引用。由于 A 是 B 公共接口的一部分,为了使用 B,您必须了解 A。对 A 的 3 个不同的公共引用,其中任何一个都需要了解 A 才能使用 B。
然而,类 C 引用了 A,但所有引用都是私有/内部/本地的。由于对 A 的每个引用都是从外部隐藏的,因此最终用户不必显式了解程序集 1。在运行时仍然需要它,但您不必将其添加为引用,它是间接引用。
如果最终用户使用类 D,而不使用 B 或 C,则只有调用 D.Foo()(它具有 A 类型的局部变量)时才会加载程序集 1。实际上,您可以自由地使用 D.Bar()即使程序集 1 在运行时完全丢失。尽管如果您调用 D.Foo() 并且程序集 1 丢失,您将得到一个异常。
What you describe is partially possible. You can eliminate the need for them to explicitly reference the hidden assembly, but that assembly will still get pulled in at compiled time, and required at runtime.
Let's say you have these classes defined:
If the end user uses class B, they will require an explicit reference to assembly 1. Since A is part of B's public interface, in order to use B, you have to know about A. There are 3 different public references to A, and any of them will require knowing about A to use B.
However, class C makes references to A, but all references are private/internal/local. Since every reference to A is hidden from the outside, the end user doesn't have to explicitly know about assembly 1. It will still be required at runtime, but you don't have to add it as a reference, it's an indirect reference.
And if the end user uses class D, without ever using B or C, assembly 1 will only get loaded if you call D.Foo(), which has a local variable of type A. You can actually use D.Bar() freely even if assembly 1 is completely missing at runtime. Although if you call D.Foo() and assembly 1 is missing, you'll get an exception.
在 C/C++ 中,类定义存在于 .h 头文件中。这使您能够引用有关类的信息(根据需要,例如当您想要从该类继承时),而无需使用包含实现信息的源文件。缺点是代码重复(.cpp 文件中的实现需要重复 .h 文件中的大部分信息)。
在.NET 世界中,设计有所不同:程序集包含类的代码(CLR 字节码)以及从该类继承所需的所有元数据(类名、有关其成员的信息等)。
这种设计的结果是,为了使用程序集 A 中定义的继承自程序集 B 中的类的类,.NET 需要 A 和 B 程序集。或者更一般地说:如果您直接或间接使用给定程序集(类、枚举、结构)中的任何内容,则需要引用该程序集。
我不确定你想阻止什么。如果您决定将代码分成两个程序集(如您所描述的那样),则无法避免引用这两个程序集。
当然,构建代码的方法有多种,但如果不知道您首先要通过将代码拆分为两个程序集来实现什么目标,就不可能提出有用的建议。
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).
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 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.
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.