MEF 用 4.5 打开通用问题 [英] MEF open generic problems with 4.5

查看:23
本文介绍了MEF 用 4.5 打开通用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 MEF Contrib 开放泛型支持,如下所示:

We were using MEF Contrib open generics support like this:

[InheritedExport]
interface ITest2<T>
{
    void Execute();
}

class TestClass2<T> : ITest2<T>
{
    public void Execute()
    {
        Console.WriteLine();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var catalog = new AssemblyCatalog(typeof(Program).Assembly);
        var container = new CompositionContainer(catalog);
        var test2 = container.GetExportedValues<ITest2<string>>();
    }
}

但是,自从安装了 .NET Framework 4.5 后,此代码不再有效.它不仅在针对 .NET 4.5 或 .NET 4.0 构建后不再有效,而且还会破坏现有的已编译应用程序.

However, since the installation of .NET Framework 4.5, this code no longer works. Not only does it no longer work after building against .NET 4.5, or .NET 4.0, but it also breaks existing compiled applications.

似乎必须在 TestClass2 上使用显式 [Export(typeof(ITest2<>))] 属性,或者更改定义:

It appears that one has to either use an explicit [Export(typeof(ITest2<>))] attribute on TestClass2, or to change the definition:

[InheritedExport(typeof(ITest2<>))]
interface ITest2<T>
{
    void Execute();
}

有谁知道为什么会发生这种变化?奇怪的是,MEF 的开放泛型支持(在 4.5 中)也因开放泛型接口上的非类型化 [InheritedExport] 属性而失败.

Does anyone know why this has changed? Curiously, MEF's open generics support (in 4.5) also fails with a non-typed [InheritedExport] attribute on an open generic interface.

我原以为 [InheritedExport] 在开放通用接口上的默认行为与 [InheritedExport(typeof(ITest2<>))] 相同.

I would have thought that the default behaviour for [InheritedExport] on an open generic interface would be the same as [InheritedExport(typeof(ITest2<>))].

谢谢,史蒂夫

推荐答案

这是开放泛型支持的 .Net 4.5 MEF 实现中的一个错误.它将在 .Net 框架的下一个版本中修复.

This is a bug in the .Net 4.5 MEF implementation of Open Generics support. It will be fixed in the next release of .Net framework.

有几种解决方法,但都不理想.

There are a couple of work arounds none of them ideal.

  1. 使接口成为抽象基类
  2. 从接口中移除 InheritedExport 并使用导出属性显式标记派生类.

我希望这会有所帮助.

这篇关于MEF 用 4.5 打开通用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆