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

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

问题描述

我们有一个大的.NET项目,包含许多模块,它们被实现为Visual Studio库项目并被编译成各自的程序集(DLL)。



我们有一个我们通过在Visual Studio项目/程序集之间设置引用来管理模块/层之间的依赖关系。这允许我们将API / SPI与实现分开,并保持不同的层分离,从而有效地实现了我们的架构的限制。



然而,我们希望我们可以将一个程序集中导入的类型重新导出到引用汇编程序集的任何进一步程序集。



例如,假设一个类型 T 在程序集中定义 A ,程序集 B 引用 A 和程序集 C 引用 B ,像这样:

  A<  -  B<  -  C 

我们想在程序集 C 中看到类型 T ,而不显式在程序集 C 中设置引用 A 。我们希望以 B 的方式重新导出 c / p>

可以吗?



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



编辑



要澄清,这里是一个简单的图。





不仅 Consumer B 依赖于 B ,而且 A ,因为 B extends A 。但是,如果 A 在与 B 的程序集不同的程序集中定义,那么我们也必须添加对 A 的装配的引用(同样适用于提供者)。但是,由于依赖于 A ,因此依赖于需要在 B B 扩展 A 。我们真正需要的是以 A 的方式重新导出 B 的依赖关系。



如何出现这种情况?假设只有一个界面 B ,你决定从中提取常用界面( A ) B 并将其放在单独的程序集中,以便其他一些模块可以重新使用新界面,而不会增加对原始界面的依赖。在那里你去:现在你必须添加一个引用到新的程序集,所有的 B 之前使用。

解决方案

.net中的程序集中有一个属性,您可以在其中告诉程序集,它期望的类型将在另一个。

  [assembly:TypeForwardedToAttribute(typeof(Example))] 

听起来像使用类型转发给我 http://msdn.microsoft.com/en-us/library/ms404275.aspx



另见您如何简单地解释类型转发?


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

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.

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

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.

Is it possible?

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.

EDIT

To clarify, here is a simple diagram.

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.

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.

解决方案

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))]

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

see also How do you explain type forwarding in simple terms?

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

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