MEF InheritedExport & Open-Generic Support

Jun 24, 2011 at 11:38 PM

Hi, I am having trouble getting the generic support to work, and was hoping someone could provide some insight on what's supported / how to operate.  Please see my simple example below:

In one assembly I have the following:

    public abstract class EntityListModel<T> : IEntityListModel<T>
    {
    }

    [InheritedExport]
    public abstract class EntityViewModel<T> : IEntityViewModel<T>
    {
        [Import]
        protected EntityViewModel<T> entityModel;
    }

    [InheritedExport]
    public interface IEntityListModel<T>
    {
    }
    
    public interface IEntityViewModel<T>
    {
    }

    public class MyBaseEntity
    {
        public string SomeData { get; set; }
        public decimal Amount { get; set; }
    }

    public class MyChildEntity : MyBaseEntity
    {
        public string ChildData { get; set; }
    }

    public class ChildEntityListVM : EntityListModel<MyChildEntity>
    {
    }

    public class MyChildEditVM : EntityViewModel<MyChildEntity>
    {
    }

Then, in my main WPF assembly, I have the following in my app.xaml.cs to load the container/parts

 

    public partial class App : Application
    {
        [Import]
        public ChildEntityListVM TestList
        { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var catalog = new DirectoryCatalog(@".\");
            var container = new CompositionContainer(catalog);
            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(this);
            container.Compose(batch);
        }            
    }

Unfortunately, at runtime I get a

 1) No exports were found that match the constraint:  ContractName MakeSenseOfMEF.ChildEntityListVM

First of all, why wouldn't my ChildEntityListVM export?  It's base interface IEntityListModel is decorated with the InheritedExport attribute.  And this same setup works in MEF 1.

Secondly, will the other Import (the entityModel field in EntityViewModel<T> be fulfilled?  This is what I'm attempting to remedy in my current v1 solution.

Please advise.

 

Thanks,

Josh

Aug 1, 2011 at 9:53 PM

The sematics of "InheritedExport" is that everything that derives from this class will be exported as this class (not as the derived class) - it has always been this way.

In your case, ChildEntityListVM will be exported indeed, but it will be exported as IEntityListModel<MyChildEntity>