Search code examples
silverlightmef

Import instance from MEF container imperatively in code in Silverlight


I'm using MEF in my Silverlight application. My code would be a lot cleaner if there were a way to "import" an instance imperatively in code (i.e. without using the Import attribute). I've been using MEF for quite some time and I have the declarative side of MEF down.

Edits:

I'm looking to do something like this:

var myObject = container.Import<MyType>();

Solution

  • You could just new up the object you want and use CompositionInitializer to satisfy any MEF dependencies in the class you are getting. This is what I do anyways :) Here's another short article on this using WPF, but I think the use is the same or very similar in silverlight.