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

查看:152
本文介绍了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框架4.5的安装,这code不再起作用。它不仅不再反对建立.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.

看来,一个人要么使用显式[导出(typeof运算(ITest2&LT;>))上TestClass2属性,或更改的定义:

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&LT;>))。

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 Framework的下一个版本。

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天全站免登陆