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("");
        }
    }
}

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

非常感谢您的帮助,

默认情况下,

为您定义的每个导出返回一个新实例。你需要在导出时调用 Shared()。这标志着该部分在整个作品中共享。

System.Composition.Convention.PartConventionBuilder.Shared()

最新更新