Choose witch exports to apply

Mar 1, 2009 at 10:43 AM
Edited Mar 1, 2009 at 10:45 AM

Hi all!

I´m working on an application were I would like to have plug-ins for “processing” texts. The main function is for a method to get name and address from a given phone number. This method is my [Import] then I´ve made different “providers”/”handlers” lets say on that gets the name and address from google, one that gets it from addresses.com and so on.

My problem is that I don’t know how to make it possible to choose witch one of thes [Export]s that are suppose to be applied?

I´ve been playing around with this code, I know that I´m not doing I right but doese someone knows who do what I´m trying to do? Is there any code-snippets that I can look at?

DirectoryCatalog ass = new DirectoryCatalog(@"..\..\extra\");
// Listing all Parts in ass.
foreach (var x in ass.Parts) {
 Console.WriteLine("Type: " + x.ToString());
}

var container = new CompositionContainer();
var batch = new CompositionBatch();

//batch.AddPart(new SimpleHallo());

SimpleHallo sh = new SimpleHallo();
Console.WriteLine("Type to create:" + sh.GetType());

//string typeName = "emTextMEF.SimpleHallo";

string typeName = sh.GetType().ToString();
Type t = Type.GetType(typeName);

SimpleHallo o = (SimpleHallo)Activator.CreateInstance(t);
Console.WriteLine("Coola stuff: " + o.Message);
batch.AddPart(o);


Thanks in adcance!

 

 

 

Mar 4, 2009 at 12:42 AM
macson, MEF is just a composition engine its job isn't really to offer choices for a users.

What I would do is create an AddressSourceProvider and Export that into the container, and have it Import your AddressSource type; it would have an collection "AddressSources", and a property "CurrentAddressSource".  Your AddressSource would have a property "Name" that you could display to the user.  Then in your application import your AddressSourceProvider and bind a dropdown list's items to the AddressSourceProvider.AddressSources property, and bind the dropdown's SelectedValue to AddressSourceProvider.CurrentAddressSource.

This way anywhere in your application that you import AddressSourceProvider you will be able to get to the currently selected AddressSource.

Josh
Mar 4, 2009 at 5:16 AM

Hi again!

I got this from a swedish forum, the approach is to put all the assembly-files i a folder. Ie "plugin". Create a DirectoryInfo object and loop through the files, bind this to a dropdown and then use the selected file in the following way:

new AssemblyCatalog(Assembly.LoadFile(Path.GetFullPath("C:\assembly.dll")));

or....

new AggregateCatalog(new [] {
  new AssemblyCatalog(Assembly.LoadFile(Path.GetFullPath("C:\assembly.dll"))),
});

Mar 8, 2009 at 9:28 PM
Josh and Macson,

I understand that it is not MEFs core purppose, but using strongly typed metadtata you can bind pretty easily. I thought a blog psot was a better place to show what I'm doing with this. You can find it at http://msmvps.com/blogs/kathleen/archive/2009/03/08/binding-to-a-mef-exportcollection.aspx

Kathleen