MEF Basics...

Sep 25, 2010 at 5:36 AM

someone put me out of my misery here. I can't even get the most basic MEF hello world app to work. Here is the complete code. _test is always null and i can never get anything wired up. what am I doing wrong?

 

 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [Import]
        public static ITest _test { getset; }
        private void Form1_Load(object sender, EventArgs e)
        {
            CompositionContainer _container = new CompositionContainer(new AssemblyCatalog(this.GetType().Assembly));
            try
            {
                _container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                System.Diagnostics.Debug.WriteLine(compositionException.ToString());
            }
            _test.test();  //_test is ALWAYS null

        }
    }

    public interface ITest
    {
        void test();
    }

    [Export(typeof(ITest))]
    public class Widget : ITest
    {

        public void test()
        {
            System.Diagnostics.Debug.WriteLine("got to widget test");
        }
    }
}
Sep 25, 2010 at 2:01 PM

errrr never mind... it was because of the static keyword.