满足自定义ExportProvider中的导入 [英] Satisfy Imports in custom ExportProvider
问题描述
我想知道如何在我的自定义ExportProvider中使用Imports.这是我要执行的操作的示例:
I'd like to know how I can have Imports in my custom ExportProvider. Here's an example of what I'm trying to do:
public class MyExportProvider : ExportProvider
{
private List<Export> _exports;
[Import()]
private IConfig _config;
public MyExportProvider()
base()
{
_exports = new List<Export>();
}
protected override IEnumerable<Export> GetExportsCore(ImportDefinition definition,
AtomicComposition composition)
{
if (!_exports.Any())
Initialize();
return _exports.Where(x => definition.IsConstraintSatisfiedBy(s.Definition);
}
private void Initialize()
{
var contractName = typeof(MyObject).FullName;
var exportDefinition = new ExportDefinition(contractName, null);
var export = new Export(exportDefinition, () => new MyObject(_config));
_exports.Add(export);
}
}
我在创建CompositionContainer时添加了提供程序.
I am adding the provider when I create the CompositionContainer.
不幸的是,导入从未被满足.我可以通过设置AllowDefaults = true看到这一点,以便创建我的提供程序,但是_config始终为空.
Unfortunately, the import is never satisfied. I can see this by setting AllowDefaults = true so my provider is created, but _config is always null.
如何配置容器和/或提供程序,以便可以满足导入要求?
How can I configure the container and/or provider so the Import will be satisfied?
推荐答案
添加导出提供程序时,仍在创建合成容器.因此,我看不到如何使用尚未创建的合成容器来导入部分自定义导出提供程序.
When you are adding your export provider you are still creating your composition container. Thus I don't see how you can use the not yet created composition container to import parts of your custom export provider.
我要做的是首先创建一个临时CompositionContainer,它将用于创建MyExportProvider.
What I would do is first create a temporary CompositionContainer that will be used to create MyExportProvider.
然后,使用MyExportProvider创建第二个最终的CompositionContainer,该应用程序的其余部分将使用它.
Afterwards use the MyExportProvider to create your second final CompositionContainer that will be used by the rest of the application.
// this is your real container, only shown here for reference
CompositionContainer container;
public void BootstrapContainerMethod()
{
// Replace this part with the catalogs required to create your export provider.
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog("./bin", "*.dll"));
// Your temporary container, declared here in local scope
// will be disposed because of using
using (var bootstrapContainer = new CompositionContainer(catalog))
{
var myExportProvider = bootstrapContainer.GetExportedValue<IMyExportProvider>();
// create your real container and optionnally add catalogs (not shown here)
container = new CompositionContainer(myExportProvider);
}
}
您还可以从另一个角度考虑问题.您是否真的需要在自定义ExportProvider中导入商品?我不知道您的要求,但也许您可以无需进口就可以解决问题.
You might also consider the problem from another angle. Do you really need to have imports in your custom ExportProvider? I do not know your requirements, but maybe you can make do without having imports.
这篇关于满足自定义ExportProvider中的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!