Importing into a Exported object with MEF

Posted by Nathan W on Stack Overflow See other posts from Stack Overflow or by Nathan W
Published on 2010-06-10T09:48:10Z Indexed on 2010/06/10 9:52 UTC
Read the original article Hit count: 271

Filed under:
|
|

I'm sorry if this question has already been asked 100 times, but I'm really struggling to get it to work.

Say I have have three projects.

  • Core.dll
    • Has common interfaces
  • Shell.exe
    • Loads all modules in assembly folder.
    • References Core.dll
  • ModuleA.dll
    • Exports Name, Version of module.
    • References Core.dll

Shell.exe has a [Export] that contains an single instance of a third party application that I need to inject into all loaded modules.

So far the code that I have in Shell.exe is:

static void Main(string[] args)
{
        ThirdPartyApp map = new ThirdPartyApp();

        var ad = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var dircatalog = new DirectoryCatalog(".");
        var a = new AggregateCatalog(dircatalog, ad);

        // Not to sure what to do here.
}

class Test
{
    [Export(typeof(ThirdPartyApp))]
    public ThirdPartyApp Instance { get; set; }

    [Import(typeof(IModule))]
    public IModule Module { get; set; }
}

I need to create a instance of Test, and load Instance with map from the Main method then load the Module from ModuleA.dll that is in the executing directory then [Import] Instance into the loaded module.

In ModuleA I have a class like this:

[Export(IModule)]
class Module : IModule
{
    [Import(ThirdPartyApp)]
    public ThirdPartyApp Instance {get;set;}
}

I know I'm half way there I just don't know how to put it all together, mainly with loading up test with a instance of map from Main.

Could anyone help me with this.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ioc-container