Problems import import MEF

I read all the questions that I can find regarding import creation issues without exporting the containing class, but I cannot find a solution to my problem. Does anyone know a way to achieve what I'm trying to do?

My modular builds have the forms and classes that they use internally. These forms need access to some exported contracts, but the import does not load because 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; }
}

Build module:

[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 tell, my options are:

  • Passing parameters to designers (pain)
  • Use dummy export for classes requiring imports that satisfy?

Any others?

+3
source share
1

SubForm . . , DoSomething , .

, , CompositionContainer ICompositionService , ICompositionService, , SubForm, ICompositionService.SatisifyImportsOnce, .

+3

Source: https://habr.com/ru/post/1748913/


All Articles