如何从引用的 .NET 程序集中重新导出类型 [英] How to re-export types from a referenced .NET assembly

查看:24
本文介绍了如何从引用的 .NET 程序集中重新导出类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个由许多模块组成的大型 .NET 项目,这些模块实现为 Visual Studio 库项目并编译为相应的程序集 (DLL).

We have a big .NET project consisting of many modules, which are implemented as Visual Studio library projects and compiled into respective assemblies (DLLs).

我们有一个分层架构,我们通过设置 Visual Studio 项目/程序集之间的引用来管理模块/层之间的依赖关系.这使我们能够将 API/SPI 与实现分开,并将不同的层分开,从而有效地加强我们架构的约束.

We have a layered architecture and we manage dependencies between the modules / layers by setting references between the Visual Studio projects / assemblies. This allows us to separate APIs/SPIs from the implementations, and keep distinct layers separated, thus effectively enforcing the constraints of our architecture.

但是,有时我们希望我们可以(传递性地)将在一个程序集中导入的类型重新导出到任何其他程序集,该程序集引用了导入程序集.

However, simetimes we wish we could (transitively) re-export the types imported in one assembly to any further assembly which has a reference to the importing assembly.

例如,假设在程序集A中定义了类型T,程序集B引用了A,并且汇编C引用B,像这样:

For example, suppose a type T is defined in assembly A, assembly B references A, and assembly C references B, like this:

A <-- B <-- C

我们希望在程序集 C 中看到"类型 T,而无需在程序集 中显式设置对 A 的引用>C.我们希望以某种方式重新导出B 中对A 的引用.

We would like to 'see' the type T in assembly C, without explicitly setting the reference to A in the assembly C. We would like to somehow re-export the reference to A in B instead.

有可能吗?

P.S. 我们为什么要这样做?因为在整个应用程序中使用了某些类型(想想实用程序/帮助程序类或通用接口),并且在依赖于这些类型的每个项目中复制引用会很烦人.此外,当我们重构代码并移动它们时,由于缺少引用,我们会收到很多错误消息.

P. S. Why do we want to do this? Because some types are used throuout the application (think of utility/helper classes or common interfaces) and it would be annoying to duplicate the references in every project which depends on these types. Furthermore, when we refactor our code and move these around, we get lots of error messages because of missing references.

编辑

为了澄清,这是一个简单的图表.

To clarify, here is a simple diagram.

不仅Consumer B依赖于B,还依赖于A,因为B扩展了A.但是,如果 A 在与 B 的程序集不同的程序集中定义,那么我们还必须添加对 程序集的引用>A(同样适用于提供者).但是没有实际需要添加这个引用,因为对A的依赖遵循的事实,即我们依赖BB 扩展了 A.我们真正需要的是以某种方式重新导出BA 的依赖.

Not only does Consumer B depend on B, but also on A, since B extends A. However, if A is defined in a different assembly than the assembly of B, then we also must add a reference to the assembly of A (the same applies to providers). But there is no actual need to add this reference, since the dependency on A follows from the fact that we depend on B and B extends A. What we really need is to somehow re-export the dependency of B on A.

这种情况怎么会出现?假设只有一个接口 B 并且您决定从 B 中提取通用接口 (A) 并将其放在一个单独的程序集中,这样一些其他模块可以重用新接口,而无需添加对原始接口的依赖.就这样:现在您必须在之前使用 B 的所有地方添加对新程序集的引用.

How can this scenario emerge? Suppose there was only one interface B and you decided to extract common interface (A) from B and place it in a separate assembly such that some other modules can reuse the new interface without adding the dependency on the original interface. And there you go: now you'll have to add a reference to the new assembly everywhere B was used before.

推荐答案

.net 中有一个关于程序集的属性,您可以通过该属性告诉程序集它期望的类型将在另一个程序集中.

There's an attribute on assemblies in .net where you can tell an assembly that the type it is expecting to be there, would be in another.

[assembly:TypeForwardedToAttribute(typeof(Example))]

对我来说听起来像是使用 Type Forwarding http://msdn.microsoft.com/en-us/library/ms404275.aspx

Sounds like a usage of Type Forwarding to me http://msdn.microsoft.com/en-us/library/ms404275.aspx

另见 你如何用简单的术语解释类型转发?

这篇关于如何从引用的 .NET 程序集中重新导出类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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