Firstly, a simple question.
Is it possible to get event when MEF (System.ComponentModel.Composition) creates an instance of the part? When this happens, I want to reflect the created object and connect various attributes. In Spring.Net, this is possible with the IObjectPostProcessor interface.
It is assumed that I am trying to implement the Publisher / Subscriber pattern in MEF. Basically the subscriber class does this:
class MyContoller { [Command("Print")] public void Print() { ... } [Command("PrintPreview")] public void PrintPreview() { ... } }
And I want to determine when an instance of MyController was created and connected to any methods that have a CommandAttribute .
A publisher, such as a menu item, will do Command.Get("Print").Fire() to post the aforementioned event.
Second question
Perhaps there is an alternative template in MEF that I miss !!!
I have seen several posts about MEF, Prism, and Aggregate events , but it looks rather complicated.
Fyi
For reference only, here is the original for the Spring.Net implementation:
class CommandAttributeProcessor : IObjectPostProcessor { static ILog log = LogManager.GetLogger(typeof(CommandAttributeProcessor)); public object PostProcessAfterInitialization(object instance, string objectName) { foreach (MethodInfo methodInfo in instance.GetType().GetMethods()) { foreach (CommandAttribute attr in methodInfo.GetCustomAttributes(typeof(CommandAttribute), true)) { if (log.IsDebugEnabled) log.Debug(String.Format("Binding method '{0}.{1}' to command '{2}'.", instance.GetType().Name, methodInfo.Name, attr.CommandName)); Command command = Command.Get(attr.CommandName); command.Execute += (EventHandler) Delegate.CreateDelegate(typeof(EventHandler), instance, methodInfo); } } return instance; } public object PostProcessBeforeInitialization(object instance, string name) { return instance; }
}