MEF - 双向导入和导出

时间:2015-12-24 09:14:14

标签: events import export mef aggregation

我使用MEF在我的项目中执行插件代码 1.我正在加载我的dll来源:

   public void AssembleComponents()
    {
        try
        {
            //Creating an instance of aggregate catalog. It aggregates other catalogs
            var aggregateCatalog = new AggregateCatalog();

            //Build the directory path where the parts will be available
            var directoryPath =
                string.Concat(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)
                            .Split('\\').Reverse().Skip(4).Reverse().Aggregate((a, b) => a + "\\" + b)
                            , "\\", "ExportComponents\\Components");


            string localPath = new Uri(directoryPath).LocalPath;
            //Load parts from the available dlls in the specified path using the directory catalog
            var directoryCatalog = new DirectoryCatalog(localPath, "*.dll");

            //Load parts from the current assembly if available
            var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            //Add to the aggregate catalog
            aggregateCatalog.Catalogs.Add(directoryCatalog);
            aggregateCatalog.Catalogs.Add(asmCatalog);

            //Crete the composition container
            var container = new CompositionContainer(aggregateCatalog);

            // Composable parts are created here i.e. the Import and Export components assembles here
            container.ComposeParts(this);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
  1. 我正在浏览我的插件并执行"验证"方法:

    public List<string> Validate(string operationType)
    {
        List<string> res = null;
        foreach (System.Lazy<IValidationRules, IPluginMetadata> plugin in ChekcsPlugins)
        {
            if (plugin.Metadata.DisplayName == operationType)
            {
                res = plugin.Value.Validate();
                break;
            }
        }
        return res;
    }
    
  2. 我知道如何在&#34;验证&#34;之后导出返回的值。已完成但我需要的是在方法执行期间返回运行时的值。 有可能吗?

0 个答案:

没有答案