In Foo.dll, derive from Bar and add an export:

[Export(typeof(IBar))]
public class ExportableBar : Bar
{
}

From: [email protected] [mailto:[email protected]] On 
Behalf Of Matt Siebert
Sent: Thursday, September 08, 2011 11:33 PM
To: ozDotNet
Subject: Manually registering an export from a MEF plugin

Hey folks,

I'm beginning to use MEF in a solution that involves a mix of .NET 3.5 and 4.0 
projects.  I'm using MEF in the 4.0 projects and I need to register some 
exports that live in the 3.5 projects.  The problem is I need to do this from 
one of the plugin projects.

To illustrate this, imagine a console application such as...


    class Program

    {

        static void Main(string[] args)

        {

            var program = new Program();



            var catalog = new DirectoryCatalog(".");

            using (var container = new CompositionContainer(catalog))

            {

                var batch = new CompositionBatch();

                batch.AddPart(program);

                container.Compose(batch);



                program.Test();

            }

        }



        [Import]

        private IFoo foo = null;



        public void Test()

        {

            Console.WriteLine(foo.Bar);

        }

    }
IFoo is in another DLL with a concrete implementation...


    public interface IFoo

    {

        string Bar { get; }

    }

    [Export(typeof(IFoo))]

    public class Foo : IFoo

    {

        [Import]

        private IBar bar;



        public string Bar

        {

            get { return "Beer " + bar.Foo; }

        }

    }
and finally, IBar is in a .NET 3.5 DLL that isn't using MEF...


    public interface IBar

    {

        string Foo { get; }

    }

    public class Bar : IBar

    {

        public string Foo

        {

            get { return "me"; }

        }

    }
Is there a way to register an IBar export without the console application 
referencing Bar.dll?

Foo.dll has a reference to Bar.dll and could register the export, but in order 
to do this composition must occur first (so that the app can discover Foo.dll) 
and that will fail since Foo's IBar import can't be satisfied.

Cheers.

Reply via email to