첫째, 간단한 질문입니다.수출 파트의 MEF 인스턴스화
MEF (System.ComponentModel.Composition)가 파트의 인스턴스를 만들 때 event
을받을 수 있습니까? 이 때 나는 생성 된 객체를 반사시키고 다양한 속성을 연결하려고합니다. Spring.Net에서 이것은 IObjectPostProcessor
인터페이스로 가능합니다.
배경은 MEF에서 Publisher/Subscriber 패턴을 구현하려고하는 것입니다.
class MyContoller
{
[Command("Print")]
public void Print() { ... }
[Command("PrintPreview")]
public void PrintPreview() { ... }
}
그리고 MyController에 인스턴스화하고 CommandAttribute
이 선까지 어떤 방법 때 감지 할 : 기본적으로 가입자 클래스는이 작업을 수행합니다.
게시자 (예 : 메뉴 항목)는 앞서 언급 한 이벤트를 게시하기 위해 Command.Get("Print").Fire()
을 게시합니다.
두 번째 질문
아마 내가 놓친 거지 MEF에서 다른 패턴이있다!
약 MEF, Prism and the Event Aggregate에 대한 일부 게시물을 보았지만 상당히 복잡해 보입니다.
은 참고
그냥 참조를 위해, 여기 Spring.Net 구현을위한 원래의 :
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;
}
}
방금 본 적이 있습니다. http://hammett.castleproject.org/index.php/2009/02/mef-exporting-and-importing-methods/ 아마도 이것이 방법입니다. –