Import property always null (MEF import issue)

前端 未结 2 774
庸人自扰
庸人自扰 2021-02-04 01:53

I try for some time to get things done using MEF but now, I run into a problem i need help.

Description: I have 2 DLL and one EXE file. ClassLibrary1 (LoggerImpl.cs, Som

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 02:11

    You need a statement like similar to the following to involve the SomeClass in the composition process

    // ClassLibrary1.dll
    //SomeClass.cs
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.Windows.Forms;
    using LogNamespace;
    
    public class SomeClass
    {
        [Import("Logging", typeof(ILogger))]
        public ILogger Log { get; set; } //<-- ALWAYS NULL ???
    
        public SomeClass()
        {
            var catalog = new AggregateCatalog();
            CompositionContainer _container;
    
            // catalog.Catalogs.Add(new DirectoryCatalog("."));
            catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
            _container = new CompositionContainer(catalog);
    
            _container.ComposeParts(this);
        }
    
        public void Print()
        {
            Log.Print();
        }
    
    }
    
    // ClassLibrary1.dll
    // LoggerImpl.cs
    namespace ClassLibrary1
    {
        [Export("Logging", typeof(ILogger))]
        public class LoggerImpl : ILogger
        {
            public void Print()
            {
                Console.WriteLine("print called");
            }
        }
    }
    
    // ClassLibrary2.dll
    // ILogger.cs
    namespace LogNamespace
    {
        public interface ILogger
        {
            void Print();
        }
    }
    
    // WindowsFormsApplication1.exe
    // WindowsFormsApplication1.cs
    namespace WindowsFormsApplication1
    {
        [Export("Form1", typeof(Form1))]
        public partial class Form1 : Form
        {
    
            [Import("Logging", typeof(ILogger))]
            public ILogger Log { set; get; }
    
            private CompositionContainer _container;
    
            public Form1()
            {
                InitializeComponent();
                Compose();
                Log.Print();
    
                SomeClass c = new SomeClass();
                c.Print();
            }
    
            private void Compose()
            {
                var catalog = new AggregateCatalog();
    
                // catalog.Catalogs.Add(new DirectoryCatalog("."));
                catalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
                _container = new CompositionContainer(catalog);
    
                try
                {
                    _container.ComposeParts(this);
                }
                catch (CompositionException compositionException)
                {
                    MessageBox.Show(compositionException.ToString());
                }
            }
        }
    }
    

提交回复
热议问题