C# MEF轻量级(系统组成)导入和导出

C# MEF轻量级(系统组成)导入和导出,c#,.net,mef,C#,.net,Mef,我想知道我是否能得到你对以下询问的指导 假设我有以下接口声明 namespace PlugInBase { public interface IPlugIn { ISharedInterface SharedInterface { get; set; } } public interface ISharedInterface { string Message { get; set; } IList<s

我想知道我是否能得到你对以下询问的指导

假设我有以下接口声明

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; }
        }
    }
}
名称空间插件库
{
公共接口IPlugIn
{
ISharedInterface SharedInterface{get;set;}
}
公共接口是共享接口
{
字符串消息{get;set;}
IList ListOfStrings{get;set;}
int Num{get;set;}
}
公共类共享接口:ISharedInterface
{
私有字符串消息;
私有IList列表字符串;
私有整数;
公共共享接口(字符串消息,IList listOfStrings,int num)
{
this.message=消息;
this.listOfStrings=listOfStrings;
this.num=num;
}
公共字符串消息
{
获取{返回消息;}
设置{message=value;}
}
公共IList列表字符串
{
获取{return listOfStrings;}
设置{listOfStrings=value;}
}
公共整数
{
获取{return num;}
设置{num=value;}
}
}
}
我有以下两个类,它们实现了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; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组成;
使用System.Composition.Convention;
使用System.Composition.Hosting;
运用系统反思;
使用系统组件模型;
名称空间插件
{
使用插件库;
公共类pluginclass:IPlugIn
{
私人信息共享接口;
公共插件类()
{
sharedInterface=新的sharedInterface(“插件消息”,新列表(新字符串[]{“testString1”,“testString2”,
“testString3”、“testString4”、“testString5”}),99);
}
公共接口共享接口
{
获取{return sharedInterface;}
设置{抛出新的NotImplementedException();}
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组成;
使用System.Composition.Convention;
使用System.Composition.Hosting;
运用系统反思;
名称空间插件
{
使用插件库;
公共类pluginclass:IPlugIn
{
私人信息共享接口;
公共接口共享接口
{
获取{return sharedInterface;}
设置{sharedInterface=value;}
}
}
}
最后,我有一个helper类,它由以下部分组成

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("");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组成;
使用System.Composition.Convention;
使用System.Composition.Hosting;
运用系统反思;
使用插件库;
使用插件;
使用PlugInB;
名称空间MainConsoleApp
{
公共类MEFComposer
{
private ContainerConfiguration ContainerConfiguration=新的ContainerConfiguration();
私有IList listOfAssemblies=新列表();
private ConventionBuilder conventions=新ConventionBuilder();
私有CompositionHost CompositionHost=null;
私有IEnumerable插件{get;set;}
公共MEFComposer()
{
//获取程序集列表
添加(Assembly.getExecutionGassembly());
添加(Assembly.Load(“PlugInA”));
添加(Assembly.Load(“PlugInB”));
添加(Assembly.Load(“PlugInBase”));
//用于构建容器的约定
conventions.ForType().Export();
conventions.ForType().Export();
conventions.ForType().ExportProperty(x=>x.SharedInterface);
conventions.ForType().ImportProperty(x=>x.SharedInterface);
//使用上面列出的程序集列表和约定构建容器。
compositionHost=容器配置。
带部件(部件列表)。
使用默认约定(约定)。
CreateContainer();
//为每个插件存储对共享接口的引用
ISharedInterface plugInASharedInterface=compositionHost.GetExport().SharedInterface;
ISharedInterface plugInBSharedInterface=compositionHost.GetExport().SharedInterface;
//打印导出器的初始值
打印共享接口内容(插件共享接口);
//打印进口商的初始值
PrintSharedInterface内容(插件SharedInterface);
//修改导出器的值
pluginSharedInterface.ListOfStrings.Add(“testString6”);
pluginSharedInterface.Num++;
pluginSharedInterface.Message=“此消息应已更改”;
//在控制台上重新打印导入程序值
PrintSharedInterface内容(插件SharedInterface);
//为什么PluginSharedInterface的值不能反映
//插件共享接口?
Console.ReadLine();
}
private void PrintSharedInterface内容(ISharedInterface sharedInterface)
{
控制台写入线(“IPlugInSharedInterface”);
Console.WriteLine(“IPlugInSharedInterface.Message:+sharedInterface.Message”);
Console.WriteLine(“IPlugInSharedInterface.Num:+sharedInterface.Num”);
WriteLine(“IPlugInSharedInterface.List of strings:”;
Console.WriteLine(“-------------------------------------------------------------”;
foreach(字符串在sharedInterface.ListOfStrings中重叠)
{
控制台。写入线(收缩);
}
Console.WriteLine(“--------------------------------------------------------------”