2015-09-26 4 views
0

Я изо всех сил, чтобы понять, почему мой код работает только тогда, когда я заменить ImportProperty линию реализатор интерфейса вместо самого интерфейса:MEF 2 SatisfyImportsOnce не работает на производные типы

[TestFixture] 
public class FlyweightTest 
{ 
    [MetadataAttribute] 
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] 
    class FlyweightIntegerKeyAttribute : ExportAttribute, IFlyweightKey<int> 
    { 
     public int Key { get; private set; } 

     public FlyweightIntegerKeyAttribute(int key) : base(typeof(IAbstraction)) { Key = key; } 
    } 

    public interface IAbstraction { } 

    [FlyweightIntegerKey(1)] 
    class Abstraction1 : IAbstraction { } 

    [FlyweightIntegerKey(2)] 
    class Abstraction2 : IAbstraction { } 

    [Test] 
    public void Test() 
    { 
     IMefFlyweight<IAbstraction, IFlyweightKey<int>> lFlyweight = new Flyweight(); 

     Initialize(lFlyweight); 

     Assert.AreEqual(2, lFlyweight.Abstractions.Count()); 
     Assert.IsTrue(lFlyweight.Abstractions.Select(lazy => lazy.Value).OfType<Abstraction1>().Any()); 
     Assert.IsTrue(lFlyweight.Abstractions.Select(lazy => lazy.Value).OfType<Abstraction2>().Any()); 
    } 

    //public interface IMefFlyweight<T, TMetadata> 
    //{ 
    // IEnumerable<Lazy<T, TMetadata>> Abstractions { get; set; } 
    //} 

    public class Flyweight : IMefFlyweight<IAbstraction, IFlyweightKey<int>> 
    { 
     public IEnumerable<Lazy<IAbstraction, IFlyweightKey<int>>> Abstractions { get; set; } 
    } 

    public void Initialize(IMefFlyweight<IAbstraction, IFlyweightKey<int>> @object) 
    { 
     var catalog = new AggregateCatalog(); 
     var registrationBuilder = new RegistrationBuilder(); 

     registrationBuilder.ForTypesDerivedFrom<IAbstraction>().Export<Lazy<IAbstraction, IFlyweightKey<int>>>(); 

     //registrationBuilder.ForTypesDerivedFrom<IMefFlyweight<IAbstraction, IFlyweightKey<int>>>().ImportProperty(flyweight => flyweight.Abstractions); 
     // This one works: 
     registrationBuilder.ForType<Flyweight>().ImportProperty(flyweight => flyweight.Abstractions); 

     foreach (var lAssembly in AppDomain.CurrentDomain.GetAssemblies()) 
      catalog.Catalogs.Add(new AssemblyCatalog(lAssembly, registrationBuilder)); 

     var container = new CompositionContainer(catalog); 

     container.SatisfyImportsOnce(@object, registrationBuilder); 
    } 

:.

registrationBuilder.ForType() ImportProperty (мухи => flyweight.Abstractions)

работает отлично, но:

registrationBuilder.ForTypesDerivedFrom >>(). ImportProperty (наилегчайшем => flyweight.Abstractions)

не хотя я считаю, что они должны быть эквивалентны в моем случае.

ответ

0

Я понял, что «импорт» работает только на классах, а не на интерфейсах.

Раствор можно импортировать все свойства:

registrationBuilder.ForTypesDerivedFrom<IImportingInterface>().ImportProperties(_ => true); 

или

registrationBuilder.ForTypesDerivedFrom<IImportingInterface>().ImportProperties<ExportClass>(_ => true);