MEF轻量级(System.Composition)导入和导出

时间:2016-04-20 05:46:07

标签: c# .net mef

我想知道我是否可以在下面的询问中得到你的指导。

假设我有以下接口声明。

namespace PlugInBase
{
    public interface IPlugIn
    {
        ISharedInterface SharedInterface { get; set; }
    }

    public interface ISharedInterface
    {
        string Message { get; set; }
        IList<string> ListOfStrings { get; set; }

        int Num { get; set; }

    }

    public class SharedInterface : ISharedInterface
    {
        private string message;
        private IList<string> listOfStrings;
        private int num;

        public SharedInterface(string message, IList<string> listOfStrings, int num)
        {
            this.message = message;
            this.listOfStrings = listOfStrings;
            this.num = num;
        }

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public IList<string> ListOfStrings
        {
            get { return listOfStrings; }
            set { listOfStrings = value; }
        }

        public int Num
        {
            get { return num; }
            set { num = value; }
        }
    }
}

我有以下2个实现IPlugin的类,并且引用了ISharedInterface。

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

namespace PlugInA
{
    using PlugInBase;

    public class PlugInAClass : IPlugIn
    {
        private ISharedInterface sharedInterface;

        public PlugInAClass()
        {
            sharedInterface = new SharedInterface("PluginA Message", new List<string>(new string[] { "testString1", "testString2",
            "testString3", "testString4", "testString5" }), 99);
        }

        public ISharedInterface SharedInterface
        {
            get { return sharedInterface; }
            set { throw new NotImplementedException(); }
        }
    }
}


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

namespace PlugInB
{
    using PlugInBase;

    public class PlugInBClass : IPlugIn
    {
        private ISharedInterface sharedInterface;
        public ISharedInterface SharedInterface
        {
            get { return sharedInterface; }
            set { sharedInterface = value; }
        }
    }
}

最后,我有一个辅助类,它按如下方式组成部分。

using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Reflection;
using PlugInBase;
using PlugInA;
using PlugInB;

namespace MainConsoleApp
{
    public class MEFComposer
    {
        private ContainerConfiguration containerConfiguration = new ContainerConfiguration();
        private IList<Assembly> listOfAssemblies = new List<Assembly>();
        private ConventionBuilder conventions = new ConventionBuilder();
        private CompositionHost compositionHost = null;
        private IEnumerable<IPlugIn> PlugIns { get; set; }

        public MEFComposer()
        {
            //Get list of assemblies
            listOfAssemblies.Add(Assembly.GetExecutingAssembly());
            listOfAssemblies.Add(Assembly.Load("PlugInA"));
            listOfAssemblies.Add(Assembly.Load("PlugInB"));
            listOfAssemblies.Add(Assembly.Load("PlugInBase"));

            //Conventions to be used to build the container
            conventions.ForType<PlugInAClass>().Export<PlugInAClass>();
            conventions.ForType<PlugInBClass>().Export<PlugInBClass>();
            conventions.ForType<PlugInAClass>().ExportProperty(x => x.SharedInterface);
            conventions.ForType<PlugInBClass>().ImportProperty(x => x.SharedInterface);   

            //Build the container with the list of assemblies and the conventions listed above.
            compositionHost = containerConfiguration.
                WithAssemblies(listOfAssemblies).
                WithDefaultConventions(conventions).
                CreateContainer();

            //Store a reference to the shared interfaces for each plugin
            ISharedInterface plugInASharedInterface = compositionHost.GetExport<PlugInAClass>().SharedInterface;
            ISharedInterface plugInBSharedInterface = compositionHost.GetExport<PlugInBClass>().SharedInterface;

            //Print initial values of the exporter
            PrintSharedInterfaceContent(plugInASharedInterface);

            //Print initial values of the importer
            PrintSharedInterfaceContent(plugInBSharedInterface);

            //Modify the values of the exporter
            plugInASharedInterface.ListOfStrings.Add("testString6");
            plugInASharedInterface.Num++;
            plugInASharedInterface.Message = "This message should have changed";

            //Reprint importer values on the console
            PrintSharedInterfaceContent(plugInBSharedInterface);

            //Why aren't the values of plugInBSharedInterface not reflecting the changes from
            //plugInASharedInterface?
            Console.ReadLine();

        }

        private void PrintSharedInterfaceContent(ISharedInterface sharedInterface)
        {
            Console.WriteLine("IPlugInSharedInterface");
            Console.WriteLine("IPlugInSharedInterface.Message: " + sharedInterface.Message);
            Console.WriteLine("IPlugInSharedInterface.Num: " + sharedInterface.Num);
            Console.WriteLine("IPlugInSharedInterface.List of strings: ");
            Console.WriteLine("--------------------------------------------------");
            foreach (string aString in sharedInterface.ListOfStrings)
            {
                Console.WriteLine(aString);
            }
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("");
        }
    }
}

在运行代码时,我期望修改pluginA(导出器)上的共享接口的值将反映在pluginB(导入器)上。不幸的是,这些值没有变化,我似乎无法弄清楚如何使用MEF来更改零件导出属性的值,以反映另一个零件的导入属性。

非常感谢你的帮助,

1 个答案:

答案 0 :(得分:0)

默认情况下,您定义的每个导出都会返回一个新实例。您需要在导出上调用Shared()。这标志着该部分在整个合成中是共享的。

System.Composition.Convention.PartConventionBuilder.Shared()