简体   繁体   中英

MEF Importing null

First of all: You all are great, I very often search this site for MEF answers.

My problem is the following:

I have several assemblies with many [Imports] in them and one main application where the assembling takes place. Now the problem is, that those Imports do not get "filled" they are always staying null.

I've tried to reproduce this behaviour in a simple small project and came up with the following source code.

Am I missunderstanding some things about MEF?

Please help! Thank you all!

Assembly Interfaces:

namespace Interfaces
{
    public interface IClass1
    {
        void Trigger();
    }

    public interface IClass2
    {
        void Trigger();
    }

    public interface IClass3
    {
        void Trigger();
    }
}

Assembly Library1:

namespace Library1
{
    [Export(typeof(IClass1))]
    public class Class1:IClass1
    {

        #region IClass1 Members

        public void Trigger()
        {

        }

        #endregion
    }
}

Assembly Library2:

namespace Library2
{
    [Export(typeof(IClass2))]
    public class Class2:IClass2
    {
        [Import]
        public IClass1 Class1 { get; set; }

        public void Trigger()
        {
        }
    }
}

In the main programm I assemble the whole Mef stuff doing the following:

namespace MEFTest
{
    public class mefStart
    {
        public CompositionContainer Container { get; private set; }

        public void Start()
        {
            AggregateCatalog catalog = new AggregateCatalog();

            AssemblyCatalog assemblyCatalog = new AssemblyCatalog(typeof(Program).Assembly);
            DirectoryCatalog directoryCatalog = new DirectoryCatalog(".", "Library*.dll");
            catalog.Catalogs.Add(directoryCatalog);
            catalog.Catalogs.Add(assemblyCatalog);

            this.Container = new CompositionContainer(catalog);

            CompositionBatch batch = new CompositionBatch();
            batch.AddExportedValue(this.Container);

            this.Container.Compose(batch);
            this.Container.ComposeParts(this);
        }
    }
}

But after that the following class does not have any of the imports filled:

namespace MEFTest
{
    public class Class3:IClass3
    {
        [Import]
        public IClass1 Class1 { get; set; }

        [Import]
        public IClass2 Class2 { get; set; }

        public void Trigger()
        {
            Class1.Trigger();
            Class2.Trigger();
        }
    }
}

When I am looking into the container, I see that the IClass1 and the ICLass2 were composed.

Why are the [Import]'s in Class3 not being satisfied? I guess I am missing something completely...

Thank you all in advance for your help!

Michael

as long as class3 is NOT instantiated by MEF you will not see any import.

btw if you do imports not via [ImportingConstructor] - be sure that the imports a satisfied (IPartImportsSatisfiedNotification) before you use them.

this would work, but i dont know where you need your class3

public class mefStart
{
    [Import]
    private IClass3 my3;
    public CompositionContainer Container { get; private set; }

    public void Start()
    {
        AggregateCatalog catalog = new AggregateCatalog();

        AssemblyCatalog assemblyCatalog = new AssemblyCatalog(typeof(Program).Assembly);
        DirectoryCatalog directoryCatalog = new DirectoryCatalog(".", "Library*.dll");
        catalog.Catalogs.Add(directoryCatalog);
        catalog.Catalogs.Add(assemblyCatalog);

        this.Container = new CompositionContainer(catalog);

        CompositionBatch batch = new CompositionBatch();
        batch.AddExportedValue(this.Container);

        this.Container.Compose(batch);
        this.Container.ComposeParts(this);

        //from here you can use Class3 with all imports
    }
}

[Export(typeof(IClass3)]
public class Class3:IClass3
{
    [Import]
    public IClass1 Class1 { get; set; }

    [Import]
    public IClass2 Class2 { get; set; }

    public void Trigger()
    {
        Class1.Trigger();
        Class2.Trigger();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM