Roman Sch

Roman Sch

  • NA
  • 7
  • 647

MEF - can't run imported function

Dec 13 2017 9:04 AM
Hi all!
 
Could You please help. In main program, i do import my plug-in in this way
  1. public static void LoadContainer(Unity.IUnityContainer container, string path, string pattern)  
  2.         {  
  3.             var dirCat = new DirectoryCatalog(path, pattern);  
  4.             var importDef = BuildImportDefinition();  
  5.   
  6.             try  
  7.             {  
  8.                 using (var aggregateCatalog = new AggregateCatalog())  
  9.                 {  
  10.                     aggregateCatalog.Catalogs.Add(dirCat);  
  11.                     using (var componsitionContainer = new CompositionContainer(aggregateCatalog))  
  12.                     {  
  13.                         IEnumerable exports = componsitionContainer.GetExports(importDef);  
  14.   
  15.                         IEnumerable modules = exports.Select(export => export.Value as IComponent).Where(m => m != null);  
  16.   
  17.   
  18.   
  19.                         //((System.ComponentModel.Composition.Primitives.ExportedDelegate)exports.Select(export => export.Value).ToArray()[0]).CreateDelegate(typeof(IComponent))  
  20.   
  21.                         try  
  22.                         {  
  23.                             var v0 = exports.Select(export => export.Value).ToArray()[0];  
  24.                             var v1 = (IComponent)v0;  
  25.                         }  
  26.                         catch (Exception ex) { ex.ToString(); }  
  27.   
  28.                         var registerComponent = new RegisterComponent(container);  
  29.                         foreach (IComponent module in modules)  
  30.                         {  
  31.                             module.SetUp(registerComponent);  
  32.                         }  
  33.                     }  
  34.                 }  
  35.             }  
where IComponent is
  1. public interface IComponent  
  2. {  
  3.     void SetUp(IRegisterComponent registerComponent);  
  4.     String Test();  
  5. }  
and exported function is
  1. class DependencyResolver : IComponent  
  2.    {  
  3.        [System.ComponentModel.Composition.Export(typeof(IComponent))]  
  4.        public void SetUp(IRegisterComponent registerComponent)  
  5.        {  
  6.            registerComponent.RegisterType();  
  7.        }  
  8.   
  9.        [System.ComponentModel.Composition.Export("mimi")]  
  10.        public string Test()  
  11.        {  
  12.            return "test)";  
  13.        }  
  14.    }  
The problem is in line
  1. IEnumerable modules = exports.Select(export => export.Value as IComponent).Where(m => m != null);    
 i got always empty list, because  export.Value is type {System.ComponentModel.Composition.Primitives.ExportedDelegate} and it can't be converted to type IComponent. Could You please advise me way to execute export.Value delegate?
 
Thanks!
 
PS I use VS2017 and full example is available at 
https://www.codeproject.com/Articles/997216/RESTful-Day-sharp-Resolve-dependency-of-dependenci

Answers (1)