我有以下使用MEF的示例代码:
public interface IFoo<T>
{}
public class Foo<T> : IFoo<T>
{}
[Export(typeof(IFoo<String>))]
public class Foo : Foo<String>
{}
public class Bar<T>
{
[Import]
private readonly IFoo<T> foo;
}
static void Main()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
container.ComposeParts();
var bar = new Bar<String>();
//bar.foo would be null
}
这似乎不起作用 - foo
字段为null
。这是因为MEF看不到它的类型是IFoo<String>
吗?