Jan 4, 2010 at 7:11 PM
Edited Jan 4, 2010 at 7:12 PM
For starters, I am using VS 2010 with .NET Framework 4 (both Beta 2). What I am attempting to do is apply the use of MEF to an existing application framework. This is an exploratory effort to see if we will be able to utilize MEF to replace an
existing factory pattern usage and remove (or highly reduce) our strongly coupled interdependencies. Our framework consists of a number of service classes that provide much of the plumbing use for building applications. Think exception
logging, security access, configuration storage/retrieval, publisher/subscriber event model, etc. In most cases, the functionality has been specialized for our environment. At the moment, these services are exposed via a factory pattern; we are
replacing the use of the factories with MEF.
The problem I've run into is an inability to access a composed part from within a static method. I have found posts that discuss the removal of support for composing static properties or fields. That's not exactly my issue (though if that
support were still in place it would solve this issue). I've run into this issue in several different situations, though all surrounding access from static methods. I'll present one which will hopefully serve as a sufficient example.
Consider an extension method that needs access to one of the framework services (aka managers). Currently, we create an instance via the appropriate factory method. The original code (before applying MEF) might look something like the following.
This code creates an instance of the Help Manager service, which is utilized to display control specific help to the user. (The details have been left out for brevity.)
In trying to apply MEF, I have modified the service class to be exported with the IHelpManager interface. This export does properly works. (I can import it via my unit testing.) However, I am having trouble finding a way to import an IHelpManager instance for use by the static method. Being an extension method, the method and class must be marked as static. MEF seems to hate the use of static in any fashion.
public static class ControlExtentionMethods
public static void ShowHelp(this Control control)
IHelpManager helpManager = ManagerFactory.GetHelpManager();
if (helpManager != null)
I'm hoping some of you long time MEF developers will be able to point me toward a solution. And I'm hoping that "don't use statics" isn't the answer. That would probably eliminate the use of MEF for us for the application.
Thanks in advance for any and all advice.