【问题标题】:Running into trouble with MEFMEF 遇到问题
【发布时间】:2012-06-03 12:24:21
【问题描述】:

我正在尝试在 .NET 4 中创建一个可扩展的“实用程序”控制台应用程序,我认为使用 MEF 来执行此操作会在灵活性和可扩展性方面给我带来最好的效果。

于是我开始设置MEF接口:

public interface IUtility
{
    string Title { get; }
    string Version { get; }

    void Execute(UtilContext context);
}

然后我创建了两个几乎相同的测试插件 - 只是为了看看这些东西是如何工作的:

MEF 插件:

[Export(typeof(IUtility))]
public class Utility1 : IUtility
{
    public string Title
    {
        get { return "Utility 1"; }
    }

    public string Version
    {
        get { return "1.0.0.0"; }
    }

    public void Execute(UtilContext context)
    {
    }
}

充当 MEF 插件“主机”的控制台应用程序如下所示:

[ImportMany]
public IEnumerable<IUtility> _utilities { get; set; }

public void SetupMEF()
{ 
   string directory = ConfigurationManager.AppSettings["Path"];

   AggregateCatalog catalog = new AggregateCatalog();
   catalog.Catalogs.Add(new DirectoryCatalog(directory));

   CompositionContainer container = new CompositionContainer(catalog);
   container.ComposeParts(this);
}

我检查过 - 正在从 app.config 正确读取目录,并且在构建解决方案后插件模块(*.dll 文件)存在于那里。一切似乎都很好......直到我得到这个例外:

System.Reflection.ReflectionTypeLoadException 未处理
消息 = 无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。

LoaderException:
程序集 'Utility1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中类型 'Utility.Utility1' 中的方法 'get_Version' 没有实现

嗯.... MEF 到底想在这里告诉我什么?我该如何解决这个问题?任何想法,想法,指针?

我拥有一个名为Version 的属性是不是打破了一些约定?这是MEF保留的东西吗?

【问题讨论】:

    标签: c# .net-4.0 mef


    【解决方案1】:

    对不起,伙计们 - 我的错。在“插件”目录中有一个非常旧的 *.dll 徘徊,该属性的 Get 方法确实没有任何实现。

    清除 pre-alpha ;-) *.dll 解决了我的问题。我现在确实可以加载我的插件,并且它们可以拥有一个名为Version 的属性而没有任何问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 2021-08-09
      相关资源
      最近更新 更多