首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MEF轻量级(System.Composition)导入和导出

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

Stack Overflow用户
提问于 2016-04-20 13:46:07
回答 1查看 794关注 0票数 2

我想知道你能不能在下面的问题上得到你的指导。

假设我有以下接口声明。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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; }
        }
    }
}

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

代码语言:javascript
复制
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 as来更改一个零件的导出属性的值,以反映在另一个零件的导入属性上。

非常感谢你的帮助,

EN

回答 1

Stack Overflow用户

发布于 2020-10-02 05:43:10

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

System.Composition.Convention.PartConventionBuilder.Shared()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36734814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档