Search code examples
c#.netwindowslegacy

Matching MEF imports to exports


In the code below I am attempting to use MEF to match an import to a matching export. The TestMEFClass has an import and an export which share a matching contract name. The export should increment a counter every time it's called.

When I printed the export to the console, it did not increment the counter. Could someone point out my mistake?

Thank you very much,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace MEFConsoleTest {

    public class TestMEFClass {

        /// <summary>
        /// This counter should increment everytime the getter in the ExportString property gets called.
        /// </summary>
        private int counter = 0;

        [Export("Contract_Name")]
        public string ExportString {

            get {
                return "ExportString has been called " + counter++.ToString();
            }
        }

        [Import("Contract_Name")]
        public string ImportString { get; set; }

        /// <summary>
        /// Default Constructor.
        /// Make a catalog from this assembly, add it to the container and compose the parts.
        /// </summary>
        public TestMEFClass() {

            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }


    }

    class Program {

        static void Main(string[] args) {

            TestMEFClass testClass = new TestMEFClass();
            Console.WriteLine(testClass.ImportString);
            Console.WriteLine(testClass.ImportString);
            Console.ReadLine();

        }
    }

Solution

  • For reasons I cannot explain at this moment I wasn't able to get MEF and properties imports/exports to work on a mutable property. However, using functions did the trick. I hope this code helps someone else.

    Thanks,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.Reflection;
    
    namespace MEFConsoleTest {
    
        public class TestMEFClass {
    
            /// <summary>
            /// This counter should increment everytime the getter in the ExportString property gets called.
            /// </summary>
            private int counter = 0;
    
            [Export("Contract_Name")]
            string ExportMethod() {
                return ExportString;
            }
    
    
            public string ExportString {
    
                get {
                    return "ExportString has been called " + counter++.ToString();
                }
            }
    
            [Import("Contract_Name")]
            Func<string> ImportMethod;
    
            public string ImportString { get { return ImportMethod(); } }
    
            /// <summary>
            /// Default Constructor.
            /// Make a catalog from this assembly, add it to the container and compose the parts.
            /// </summary>
            public TestMEFClass() {
    
                AggregateCatalog catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
    
    
        }
    
        class Program {
    
            static void Main(string[] args) {
    
                TestMEFClass testClass = new TestMEFClass();
    
                for (int x = 0; x < 10; x++) {
                    Console.WriteLine(testClass.ImportString);
                }
    
                Console.ReadLine();
    
            }
        }
    }