我有一种
public class Queue<T> : IQueue
{
....
}我可以用
[Export(typeof(Queue<>))],这将使我能够用
[Import] Queue<StockController>
[Import] Queue<FileController>但是,我想将这些类型导入为
[ImportMany] IQueue如果不为每个队列创建具体类型,这似乎是不可能的。
我希望使用约定来提供这些导出,并向每个导出中添加元数据。我不知道如何添加元数据,但我不知道如何提供导出。本质上,我想要的是(伪代码)
conventions.
ForType(typeof(Queue<StockContoller>)).Export<IQueue>(x => x.AddMetadata("Name", "StockQueue")
ForType(typeof(Queue<FileContoller>)).Export<IQueue>(x => x.AddMetadata("Name", "FileQueue")但是,除非我为每个队列创建具体类型,否则这是行不通的,这是我不想做的。
有办法这样做吗?
发布于 2014-12-15 10:38:27
结果是解决方案分为两部分。首先,将部件添加到容器中,然后定义约定。以下是实现这一目标的扩展方法。
public static ContainerConfiguration WithQueue(
this ContainerConfiguration config,
Type queueType,
string queueName)
{
var conventions = new ConventionBuilder();
conventions
.ForType(queueType)
.Export<IQueue>(x => x.AddMetadata("QueueName", queueName));
return config.WithPart(queueType, conventions);
}用法:
CompositionHost container =
new ContainerConfiguration()
.WithQueue(typeof(ControllerQueue<StockMovementController>), "StockMovements")
.WithAssemblies(GetAssemblies())
.CreateContainer();(其中GetAssemblies()返回用于构建容器的程序集)
https://stackoverflow.com/questions/27442296
复制相似问题