当使用
RegistrationBuilder
时,下面的代码不起作用。当未将RegistrationBuilder
添加到AssemblyCatalog构造函数中时,类型约束泛型可正常工作。[TestClass]
public class TypeConstraints
{
[TestMethod]
public void TypeConstraintTest()
{
var rb = new RegistrationBuilder();
var a = new AssemblyCatalog(Assembly.GetExecutingAssembly(), rb);
//var a = new AssemblyCatalog(Assembly.GetExecutingAssembly()); //Works!
var aggr = new AggregateCatalog(a);
var c = new CompositionContainer(aggr);
var item = c.GetExportedValue<IConstrained<Item>>();
Assert.IsNotNull(item);
}
}
public interface IConstrained<T> where T : IItem
{}
[Export(typeof (IConstrained<>))]
public class Constrained<T> : IConstrained<T> where T : IItem
{}
public class Item : IItem
{}
public interface IItem
{}