[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]
I’m pretty pumped.. I figured out how to hack my way around a couple issues to get WP7 to get MEF working. I also figured out how to get the CompositionInitializer working as well. Mostly I removed all the reflection stuff via “#if !WINDOWS_PHONE” directives and the in the “#else” I provide alternative source. I honestly have barely tested it so be aware (but it should be fun to play with).
Silverlight Dev !=WP7 Dev
I mentioned that I hacked my way to getting the CompositionInitializer working (which is part of the source code in the last preview for MEF 1). The biggest problem is that you can’t walk the assemblies in Silverlight; in Silverlight you do walk the deployment package and load each assembly (which they are probably already loaded so this becomes “no operation” that returns a reference to the already loaded assembly). All of this is fine and well EXCEPT WP7 doesn’t allow you to load assemblies (part of the security model). You can inherit from Assembly –and the other reflection stuff—but you can’t instantiate any of this.
T4 to the rescue
So I created a T4 template that lets C# developers (sorry VB guys.. I’ll get to you soon) walk the project references associated with the main project and it finds a public type that can be used at runtime to get a reference to the underlying Assembly and adds this assembly to my StaticAssemblyResolver class which provides a list of Assemblies in place of the Assembly walking code; this is the default for my WP7 version of MEF.
How do I use it
I wanted to minimize the effort here. so you have to add a single line of code in your App’s startup code:
The only other thing you need to do is include my T4 template which is located in the Samples\WP7-SimpleViewModel project. (the T4 template might need to be run after adding a new project, but it should make all your Source Project assemblies available).
This should cover most of you. ext up for me will be to make the T4 work produce VB code as well as I want it to be able to walk the DLLs that are added to the project and get those assemblies added.
I have actually started trying to get some of the other MEF samples working as well (although a number of things will never work).
BTW, the source is still a little rough around edges for some of the stuff I’ve been actively working on.. much of the core was pretty simple.. it’s the stuff where I had to figure out how to help MEF find assemblies or get around WP7 reflection limitations.
Here’s the bits:
MEF 2 Preview1 For WP7