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:
- Pass parameters to constructors (pain)
- Use a dummy export on the classes that need imports satisfying?
Any others?