tags:

views:

358

answers:

1

hi there.

i am currently working with MEF and facing some problems

what i want is to load dlls from the directory.

first i scan the directory and save two things in dictionary

Name property from respective DLL (as string)

and Module Name (as string)

here is ScanDirectory() code

 private void ScanPluginDirectory()
        {
            catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(@"..\..\plugin"));            
            container = new CompositionContainer(catalog);
            batch = new CompositionBatch();
            batch.AddPart(this);            
            container.Compose(batch);    
            pluginDictionary = new Dictionary<String, String>();
            foreach (IFilter filter in filters)
            {
                Type t = filter.GetType();
                pluginDictionary.Add(filter.Name, t.Module.Name);
            }
        }

and show their name in a checkbox list. upon selection of dll from checkbox.

i have import statement as

[Import]
        public IEnumerable<IFilter> filters { get; set; }

currently my program is running fine. what i did is when i check a plugin from checkbox list. it moves it into "loaded" directory and and they QueryPlugin() method looks into "loaded" directory to search for plugins.

upon unchecking plugin from checkbox list. i move it out of "loaded" directory...

What i want is to use batch.RemovePart() method to get rid of this rapid moving of dlls from one directory to other....

NOTE: i am not adding plugins manually into batch using

batch.AddPart(new DemoFilter1());

instead of this i used DirectoryCatalog();;

+1  A: 

Instead of using a DirectoryCatalog, use an AggregateCatalog and add an AssemblyCatalog for each assembly in the directory to the aggregate catalog. Then when a plugin is checked or unchecked, you can add or remove the corresponding AssemblyCatalog to the AggregateCatalog.

Note that there may be problems with this approach if there is more than one plugin in a given assembly. A more robust approach could be filtering individual part definitions using a filtered catalog.

Daniel Plaisted