我想手动创建组合导出,但 Export 构造函数仅接受元数据字典,而我应该直接从元数据对象 (IMyMetadata) 创建它。
没有办法从对象创建字典吗?像这样:
IDictionary<string, object> metadata = AttributedModelServices.GetMetadataDictionary(metadata)
其中元数据是 IMyMetadata 对象。
本质上,我的需要是使用导入的 IEnumerable>要创建新的合成,请将值“转发”到另一个 CompositionBatch。
谢谢
I would like to manually create exports for composition, but the Export constructor only accepts a dictionary for metadata, while I should directly create it from the metadata object (IMyMetadata).
Isn't there a way to create the dictionary from the object? Something like this:
IDictionary<string, object> metadata = AttributedModelServices.GetMetadataDictionary(metadata)
where metadata is an IMyMetadata object.
Essentially, my need is that of using an imported IEnumerable> to create new compositions, sort of "forwarding" values to another CompositionBatch.
Thank you
发布评论
评论(3)
当然可以,像这样:
听起来您正在尝试实现某种 "ExportMany" 功能, MEF 中(还?)不存在。 (它确实存在于其他一些 IOC 容器中,例如 AutoFac 中的轻量级适配器)。您也许可以不需要这样的功能。
我假设您有某种类型的导出 (
IStuff
) 可用,但您需要将它们包装在适配器中,以便可以将它们作为其他类型导入 (IAdaptedStuff
) )。也许您可以坚持正常的属性编程模型并执行以下操作:Sure, like this:
It sounds like you're trying to implement sort of a "ExportMany" feature, which does not (yet?) exist in MEF. (It does exist in some other IOC containers, like lightweight adapters in AutoFac). You may be able to do without such a feature.
I'm going to assume you have exports of some type (
IStuff
) available, but you need to wrap them in adapters so that you can import them as something else (IAdaptedStuff
). Maybe you could stick to the normal attributed programming model and do something like this:您可以导入
IEnumerable>
来获取原始元数据字典。听起来这对你有用。You can import
IEnumerable<IContract, IDictionary<string, object>>
to get the raw metadata dictionary. It sounds like this would work for you.例如:
For example: