MEF Import Composition Issues

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2010-06-07T20:00:00Z Indexed on 2010/06/08 14:12 UTC
Read the original article Hit count: 243

Filed under:
|
|

I've read all the questions I can find regarding the issues of composing imports without exporting the containing class but I can't find a solution to my problem. Does anybody know a way to achieve what I'm trying to do?

My module assemblies have forms and classes which they use internally. These forms need access to some of the exported contracts but imports are not loaded as they are not in the MEF 'composition tree'

Host assembly:

public class Host
{
    public Host()
    { /* Compose parts here... */ }

    [Export(typeof(Licence))]
    public Licence LoadedLicence { get; set; }  

    [Export(typeof(IModule))]
    public List<IModule> LoadedModules { get; set; }
}

Module assembly:

[Export(typeof(IModule))]
public class Module : IModule
{        
    public Module() { }

    public void DoSomething()
    {
        SubForm sub = new SubForm();
        sub.ShowDialog();
    }

    [Import(typeof(Licence))]
    public Licence LoadedLicence { get; set; } // This works here
}

public class SubForm : Form
{        
    public SubForm ()
    { }

    [Import(typeof(Licence))]
    public Licence LoadedLicence { get; set; } // This doesn't work in here
}

As far as I can see, my options are:

  1. Pass parameters to constructors (pain)
  2. Use a dummy export on the classes that need imports satisfying?

Any others?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET