重写MEF ExportFactory方法以使用泛型?

时间:2019-05-02 01:53:52

标签: c# mef

在我的程序中,我正在使用MEF进行插件操作,可以在运行时从dll库读取内容,并在选择选项后选择特定的实现。

[ImportMany]
        public IEnumerable<ExportFactory<IMyInterface, Dictionary<string, object>>>
            MyFactories { get; set; }


public IMyInterface GetImplementation()
        {
            try
            {
                ExportLifetimeContext<IMyInterface> selectedFactory =
                    MyFactories.Single(e => (string) e.Metadata["Name"] == _selectedName).CreateExport();
                return selectedFactory.Value;
            }

            catch (InvalidOperationException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

它按原样工作,但是我想对另一个接口做同样的事情。 最简单的方法是复制\粘贴并将IMyInterface更改为IAnotherInterface,但是我一直在尝试确定是否可以重写此代码 因此我可以将接口类型\ string传递给GetImplentatuin。我想我应该使用泛型,但是还无法弄清楚该怎么做。 我还不太了解它们。我

0 个答案:

没有答案