首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有元数据的MEF2轻量级System.Composition

带有元数据的MEF2轻量级System.Composition
EN

Stack Overflow用户
提问于 2019-08-29 11:12:20
回答 1查看 864关注 0票数 0

由于完全缺乏如何使用轻量级MEF2,System.Composition的示例,这使得这个问题变得棘手。我只使用System.Composition (不使用System.ComponentModel.Composition)。

我想导入具有元数据的部分。我用的是属性代码。不幸的是,当我试图得到出口,我得到了一个大的脂肪零。

The MetadataAttribute

代码语言:javascript
复制
namespace Test.ConfigurationReaders
{
    using System;
    using System.Composition;

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class ExportReaderAttribute : ExportAttribute
    {
        public string Reader { get; set; }
    }
}

Export

代码语言:javascript
复制
namespace Test.ConfigurationReaders
{
    using System.Collections.Generic;
    using System.Configuration;

    [ExportReader(Reader = "CONFIG")]
    public class ConfigReader : IConfigurationReader
    {
        public IEnumerable<Feature> ReadAll()
        {
            return new List<Feature>();
        }
    }
}

元数据的ImportMany和过滤器:

代码语言:javascript
复制
namespace Test.Core
{
    using System;
    using System.Collections.Generic;
    using System.Composition;
    using System.Composition.Hosting;
    using System.Configuration;
    using System.Linq;
    using System.Reflection;
    using ConfigurationReaders;

    public sealed class Test
    {
        static Test()
        {
            new Test();
        }

        private Test()
        {
            SetImports();
            Reader = SetReader();
        }

        [ImportMany]
        private static IEnumerable<ExportFactory<IConfigurationReader, ExportReaderAttribute>> Readers { get; set; }

        public static IConfigurationReader Reader { get; private set; }

        private static IConfigurationReader SetReader()
        {
            // set the configuation reader based on an AppSetting.
            var source =
                ConfigurationManager.AppSettings["Source"]
                ?? "CONFIG";

            var readers = Readers.ToList();

            var reader =
                Readers.ToList()
                .Find(
                    f =>
                    f.Metadata.Reader.Equals(
                        source,
                        StringComparison.OrdinalIgnoreCase))
                    .CreateExport()
                    .Value;

            return reader;
        }

        private void SetImports()
        {
            var configuration =
                new ContainerConfiguration().WithAssemblies(
                    new List<Assembly> {
                        typeof(IConfigurationReader).Assembly });
            var container = configuration.CreateContainer();
            container.SatisfyImports(this);
            Readers = container.GetExports<ExportFactory<IConfigurationReader, ExportReaderAttribute>>();
        }
    }
}

不幸的是,Readers是空的。这里是示例代码,但我可以看到实际代码中没有元数据的部分,这样至少可以工作。

我该怎么办才能填充Readers

我试图瞄准.NET标准2.0,并从.NET内核中使用它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-30 11:10:37

解决方案是更改导出类的属性:

代码语言:javascript
复制
    [Export(typeof(IConfigurationReader))]
    [ExportMetadata("Reader", "CONFIG")]
    public class ConfigReader : IConfigurationReader
    {
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57708910

复制
相关文章

相似问题

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