ImportMany stopped working after some modifications

Dec 30, 2011 at 5:50 PM

I have an application with 2 modules. In main there was import

  [ImportMany(typeof(IModule))]
  IEnumerable<Lazy<IModule, IModuleMetadata>> _modules;
Then I created new class Menu in main module, with same namespace. I moved 
the import lines into the new class and from now is _modules always null. When
I leave the lines in main, _modules contains 2 elements. Then after creating
Menu and debugging into the class I see _modules is null.
What could be the reason for it?
Regards, Gabriel
Dec 30, 2011 at 5:55 PM

Hi Gabriel,

How are you creating the Menu part – does it have exports?

Regards,
Nick

From: Gabriel56 [email removed]
Sent: Friday, December 30, 2011 10:51 AM
To: Nicholas Blumhardt
Subject: ImportMany stopped working after some modifications [MEF:284602]

From: Gabriel56

I have an application with 2 modules. In main there was import

  [ImportMany(typeof(IModule))]
  IEnumerable<Lazy<IModule, IModuleMetadata>> _modules;
Then I created new class Menu in main module, with same namespace. I moved 
the import lines into the new class and from now is _modules always null. When
I leave the lines in main, _modules contains 2 elements. Then after creating
Menu and debugging into the class I see _modules is null.
What could be the reason for it?
Regards, Gabriel
Dec 30, 2011 at 6:05 PM

Hi Nick,

thank you for the quick answer.

This ist the part of main class:

    [Export(typeof(MainWindowViewModel))]
    public class MainWindowViewModel : NotifyPropertyChangedBase
    {
        //[ImportMany(typeof(IModule))]
        //IEnumerable<Lazy<IModule, IModuleMetadata>> _modules;

        MainWindow mainWindow = null;

        MenuElement m = null;

        public MainWindowViewModel()
        {
        }

        void LoadMenu()
        {
            string fn = @"data\menu.xml";
            if (File.Exists(fn))
            {
                m = new MenuElement();
                m.LoadFromFile(fn);
            }
        }

and here the MenuElement class:
        [ImportMany(typeof(IModule))]
        IEnumerable<Lazy<IModule, IModuleMetadata>> _modules;

        public void Expand()
        {
            if (Children != null)
            {
                foreach (var submenu in Children)
                    submenu.Expand();

                var apps = _modules.Where(x => x.Metadata.Group == Name);
                Children.AddRange(apps
                    .Select(x => new MenuElement
                    {
                        Name = x.Metadata.Name,
                        IconFilename = x.Metadata.IconFilename,
                        Parent = this,
                        Children = null,
                    })
                );
            }
        }

        public void LoadFromFile(string filename)
        {
            XDocument doc = XDocument.Load(filename);
            XElement root = doc.Element("main");

            ParseMenu(null, root);
            Name = "Main";
            Expand();
        }


The MenuElement class has no exports, but I tried to declare dummy export without results.
Regards, Gabriel
Dec 30, 2011 at 6:16 PM

I've solved it. Now MenuElemnt exports itself to main class and in that calls only m.LoadFromFile(...). Now I can see the plugins.

Regards, Gabriel

Dec 30, 2011 at 6:20 PM

Sorry, it doesn't work. The first level of recursion is ok, but after calling Expand() on submenu are _plugins null.

Regards, Gabriel