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

查看:185
本文介绍了如何再出口从引用的.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.

不过,simetimes我们希望我们能(及物动词)再出口于一体的组装进口的种类,任何进一步的组件,它引用了进口组装。

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.

例如,假设一个类型 T 的组件定义 A ,组装 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

我们想'看' T 组装类型 C ,没有明确设定参照 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.为什么我们要这么做?由于某些类型用于throuout应用(想想工具/辅助类或普通接口),这将是恼人的重复在每一个项目,这取决于这些类型的引用。此外,当我们重构我们的code和周围移动这些,我们得到很多,因为缺少引用错误信息。

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.

不仅消费者乙依赖于 B ,也是对 A ,因为 B 扩展 A 。但是,如果 A 被定义在不同的装配比 B 的组件,那么我们也必须添加引用 A 的组件(同样适用于供应商)。但没有实际需要以添加此引用,因为对 A 依赖如下从我们赖以生存的事实在 B B 扩展 A 。我们真正需要的是某种程度上的转口 B A 的依赖。

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 ,你决定提取通用接口( 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))]

听起来像键入转发来我的 http://msdn.microsoft.com/en-us/library/ms404275.aspx

又见<一href="http://stackoverflow.com/questions/736968/how-do-you-explain-type-forwarding-in-simple-terms">How你深入浅出的讲解转发类型?

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

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